Hi Patrick,
try this out on a COPY of your data...
Option Base 1
Public Sub SoldBooks()
Const strSoldTitlesColumn As String = "A"
Const strSoldTitlesHeading As String = "Sold Titles"
Dim vaData As Variant
Dim vaSoldBooks() As String
Dim iLastRow As Long
Dim iLAstRowbd As Long
Dim iLastColumn As Integer
Dim iHeading As Integer
Dim iBookCount As Long
Dim iSoldCounter As Long
Dim iSoldColumn As Integer
Dim iTitleColumn As Integer
'Find the right-most column heading column,
'bottom-most data row and load vaData array with
'all the data on sheet named "extended"
With Worksheets("extended")
iLastColumn = _
..Cells(1, Range("1:1").Columns.Count).End(xlToLeft).Column
iLastRow = .Range("A" & .Range("A:A").Rows.Count).End(xlUp).Row
vaData = .Range(.Cells(1, 1), .Cells(iLastRow, iLastColumn))
End With
'Find the "SOLD" and "TITLE" headings in row 1 of the
'vaData array. Assign their column numbers to iSoldColumn and
'iTitleColumn respectively.
For iHeading = 1 To iLastColumn
Select Case UCase(vaData(1, iHeading))
Case "SOLD"
Let iSoldColumn = iHeading
Case "TITLE"
Let iTitleColumn = iHeading
End Select
If iSoldColumn And iTitleColumn <> 0 Then Exit For
Next iHeading
'If "SOLD" heading not found, tell user then Exit Sub
If iSoldColumn = 0 Then
MsgBox "Can't find 'Sold' Column"
Exit Sub
End If
'If "TITLE" heading not found, tell user then Exit Sub
If iTitleColumn = 0 Then
MsgBox "Can't find 'Title' Column"
Exit Sub
End If
'Search "SOLD" column for "YES" values - NOT Case Sensitive
For iBookCount = 1 To iLastRow
If UCase(vaData(iBookCount, iHeading)) = "YES" Then
'"YES" value found
Let iSoldCounter = iSoldCounter + 1
ReDim Preserve vaSoldBooks(iSoldCounter)
'Load vaSoldBooks array with Sold Book's Title located
'in same row of the column(iTitleColumn)
vaSoldBooks(iSoldCounter) = vaData(iBookCount, iTitleColumn)
End If
Next iBookCount
With Worksheets("breakdown")
'Clear the column to receive the Sold Book Titles
..Columns(strSoldTitlesColumn).ClearContents
'Replace the "Sold Titles" heading
..Range(strSoldTitlesColumn & "1").Value = _
strSoldTitlesHeading
'Add the Titles of the Sold Books
..Range(strSoldTitlesColumn & "2"). _
Resize(UBound(vaSoldBooks), 1) _
..Value = WorksheetFunction.Transpose(vaSoldBooks)
..Columns(strSoldTitlesColumn).AutoFit
End With
End Sub
I have added comments to help with interpreting what I have done.
I have coded out the dependance on the relative positions of "Sold" and
"Title" headings.
The code now searches for both headings in row 1 so it doesn't matter
where they are positioned, as long as they are somewhere in row 1 and
are spelt "sold" and "title".
I haven't completely coded out the final column for the sold titles on
the breakdown sheet, but I have made it very easy for you to change it
in one fell swoop.
Look at the first two lines of code...
Const strSoldTitlesColumn As String = "A"
Const strSoldTitlesHeading As String = "Sold Titles"
the "A" results in the titles in column A on the breakdown sheet, so
all you need to do is change it to any thing from "B" up to "IV". What
could be easier?
The "Sold Titles" can also be changed to whatever you heart desires, it
determines the heading you will see at the top of the list of sold
titles.
I've also added an AutoFit line so that the Sold Titles column width
adjusts its width to accommodate the longest title.
Ken Johnson