I have data in I1 and J1 on sheet BR1 Shirt Sales to Last Sheet
I need to copy this data and paste these as values one below each other in Cols I and J on sheet Consolidated
I have tried to write code to do this but it is only pasting the data in Col I1 and J1 from sheet BR1 Shirt Sales
It would be appreciated if someone could amend my code
I need to copy this data and paste these as values one below each other in Cols I and J on sheet Consolidated
I have tried to write code to do this but it is only pasting the data in Col I1 and J1 from sheet BR1 Shirt Sales
It would be appreciated if someone could amend my code
Code:
Sub ExtractDataCols_IandJ()
Dim lastRow As Long
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet
Dim targetRow As Long
Dim sourceRow As Long
' Set the target sheet to Consolidated
Set targetSheet = ThisWorkbook.Sheets("Consolidated")
' Clear the data in column I and J on the target sheet
targetSheet.Range("I:I,J:J").ClearContents
' Set the initial target row to 1
targetRow = 1
' Loop through each sheet in the workbook
For Each sourceSheet In ThisWorkbook.Sheets
' Check if the sheet name starts with "BR1 Shirt Sales"
If Left(sourceSheet.Name, 24) = "BR1 Shirt Sales" Then
' Copy the data in cells I1 and J1 from the source sheet to the target sheet
targetSheet.Range("I" & targetRow).Value = sourceSheet.Range("I1").Value
targetSheet.Range("J" & targetRow).Value = sourceSheet.Range("J1").Value
' Find the last row in column I of the source sheet
lastRow = sourceSheet.Cells(Rows.Count, "I").End(xlUp).Row
' Loop through each row in columns I and J of the source sheet
For sourceRow = 2 To lastRow
' Copy the data in column I and J from the source sheet to the target sheet
targetSheet.Range("I" & targetRow + sourceRow - 1).Value = sourceSheet.Range("I" & sourceRow).Value
targetSheet.Range("J" & targetRow + sourceRow - 1).Value = sourceSheet.Range("J" & sourceRow).Value
Next sourceRow
' Increment the target row by the number of rows copied from the source sheet
targetRow = targetRow + lastRow - 1
End If
Next sourceSheet
' Select cell A1 on the target sheet
targetSheet.Range("A1").Select
End Sub