The way my data is setup is as follows:
Master Sheet:
Column A: Company Names (rows 1-100)
Column B: Flag? Y/N (rows 1-100)
Sheet2:
Column A: Company Names (rows 1-100 in the same order as Master Sheet)
Columns B-D: Data for each company (one company's data per row)
Sheet 3/4 are the same as Sheet2, just different data.
What I'm trying to do:
If on the Master Sheet Column B's value is "Y", then for that row, copy the same row from Sheets 2/3/4 into a new workbook.
Example:
In the Master Sheet, the first row with a "Y" value is row 6 (therefore, company#6). I would like to copy row 6 from sheets 2/3/4 into a new workbook with 4 sheets (Master Sheet row 6 only, Sheet2/3/4 row 6 only).
Repeat for all rows with a Y value.
The code I have so far:
Dim wb As Workbook, FileNm As String, LastRow As Long, wbTemp As Workbook, k As Long, currentRow As Long
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Set wb = ThisWorkbook
With wb
LastRow = .Sheets(1).Cells(.Sheets(1).Rows.Count, 1).End(xlUp).Row
End With
For k = 1 To LastRow
FileNm = wb.Path & "\" & "BOOK" & k & ".xlsx" 'adding the i to number every workbook from 1 to number of rows
Set wbTemp = Workbooks.Add 'adding a new workbook
If Worksheets("Master Sheet").Cells(k,2).Value = "Y" Then
currentRow = Worksheets("Master Sheet").Rows(k)
wb.Sheets(1).currentRow.Copy Destination:=wbTemp.Sheets(1).Rows(1)
wb.Sheets(2).currentRow.Copy Destination:=wbTemp.Sheets(2).Rows(1)
wb.Sheets(3).currentRow.Copy Destination:=wbTemp.Sheets(3).Rows(1)
wb.Sheets(4).currentRow.Copy Destination:=wbTemp.Sheets(4).Rows(1)
End If
wbTemp.SaveAs FileNm
wbTemp.Close
Set wbTemp = Nothing
Next k
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
It's not working-- my guess is due to the line
currentRow = Worksheets("Master Sheet).Rows(k)
but I don't know how to work around that. Any help appreciated.
Thank you.
Rows(11)
notK
– Badjak
– urdearboy("Master Sheet").Rows
? You're missing a"
and it's messing the rest of the snippet up – Badjak
as a variable - try hiding that line and running it again - what do you get? – Badja