Hi Kim,
I get an error message, argument not optional
I updated the sheet name to Accounts, changed the 2 ranges first one to b2:s429 and the second range to a2:s2. The name of the sheet I am running the macro from is 49210 which is also in column 2 several times.
Also, how come copy data does not show up on the list of macros... I have to type in the name... like hidden....
I appericate your insight and comments
SR
Sub copydata(sheetname As String)
Dim SheetData As String
Dim DataRowNum As Integer, SheetRowNum As Integer
SheetData = "Accounts" ' Source sheet
DataRowNum = 2 ' Begin search at row 2
SheetRowNum = 2 ' Begin saving data to row 2 in "Sheet 1"
' Select sheetname, as its apparently required before copying is allowed!
Worksheets(SheetData).Select
' Search and copy the data
While Not IsEmpty(Cells(DataRowNum, 2)) ' Loop until column B gets blank
' Search in column B for our value, which is the same as the target sheet name "sheetname"
If Range("B2:S429" & CStr(DataRowNum)).Value = sheetname Then
' Select entire row
Rows(CStr(DataRowNum) & ":" & CStr(DataRowNum)).Select
Selection.Copy
' Select target sheet to store the data "sheetname" and paste to next row
Sheets(sheetname).Select
Rows(CStr(SheetRowNum) & ":" & CStr(SheetRowNum)).Select
ActiveSheet.Paste
SheetRowNum = SheetRowNum + 1 ' Move to next row
' Select source sheet "SheetData" so searching can continue
Sheets(SheetData).Select
End If
DataRowNum = DataRowNum + 1 ' Search next row
Wend
' Search and copying complete. Lets make the columns neat
Sheets(sheetname).Columns.AutoFit
' Finish off with freezing the top row
Sheets(sheetname).Select
Range("A2:S2").Select
ActiveWindow.FreezePanes = True
End Sub