Originally Posted by
Gabba
This has to be deleted when no record found is displayed.
Ok, then, move the statement to after the sheet has been cleared.[vba] 'Clear the sheet.
Application.ScreenUpdating = False
Application.EnableEvents = False
Range("B4:B20").ClearContents
If Cells(3, 3) <> vbNullString Then Range(Cells(3, 3), Cells(20, Cells(3, 2).End(xlToRight).Column)).Delete (xlShiftToLeft)
If records.Count = 0 Then MsgBox "No records found for '" & Target & "'.": GoTo exitSub '<--Here's a better place for this line.[/vba]
Originally Posted by
Gabba
Also want to know what the below code does:[vba]For Each c In .Range(.Cells(2, 1), .Cells(.Cells(2, 1).End(xlDown).Row, 1))
If c = Target Then records.Add c.Address[/vba]
This loops through all the names on Sheet3 ("RawData") and fills a collection of the addresses of all the cells that have the correct name.
Originally Posted by
Gabba
Also want to know what the below code does:
...[vba]For i = 1 To records.Count
Paste (Cells(3, 1 + i))
Cells(3, 1 + i) = "Data" & i
For ofset = 1 To 17
Cells(3, 1 + i).Offset(ofset) = Sheets("RawData").Range(records(i)).Offset(, ofset)
Next
[/vba]
This loops through all the items in the collection, records, makes a new set of formatted cells (copied from Range("B3:B20")) to accept the following data, puts a header at the top of the column ("Data" + the current iteration of i), and goes down the rows of the column, filling them with data from your sheet, "RawData".