Macro to Copy Data from Sheet BR1 Shirt Sales to Last Sheet

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
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

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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I don't quite understand why you're using a loop when you could just copy the entire range in one go. Does this achieve what you're looking for?

VBA Code:
Option Explicit
Sub Howard()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("BR1 Shirt Sales")
    Set ws2 = Worksheets("Consolidated")
    
    ws2.Range("I:J").EntireColumn.ClearContents
    ws1.Range("I1:J" & ws1.Cells(Rows.Count, "J").End(xlUp).Row).Copy
    ws2.Range("I1").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    Application.Goto ws2.Range("A1")
End Sub
 
Upvote 0
In case columns I & J are of different lengths on the source sheet:

VBA Code:
Option Explicit
Sub Howard_2()
    Dim ws1 As Worksheet, ws2 As Worksheet, LRow As Long
    Set ws1 = Worksheets("BR1 Shirt Sales")
    Set ws2 = Worksheets("Consolidated")
    LRow = ws1.Range("I:J").Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    
    ws2.Range("I:J").EntireColumn.ClearContents
    ws1.Range("I1:J" & LRow).Copy
    ws2.Range("I1").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    Application.Goto ws2.Range("A1")
End Sub
 
Upvote 0
Thanks for the help. The data is only being copied from sheet "BR1 Shirt Sales" I need the data to copy the data in Cols I & J from sheet "BR1 Shirt Sales" to the last sheet and paste in Col I and J on sheet "Consolidated"

Kindly amend code accordingly
 
Upvote 0
Does the "last sheet" have a name, or is it simply the last sheet in the workbook? Also, do you want it copied to columns I:J in the last sheet? Just so I'm totally clear, you want columns I:J from the BRI Shirt Sales sheet to both the Consolidated sheet AND the last sheet as well?
 
Upvote 0
I want the data copied from Sheets BR1 Shirt Sales up to the last sheet in the workbook and pasted on sheet Consolidated in ColI an J one below each Other
 
Upvote 0
So for example, if there's 5 sheets, the first one is called Consolidated, the second one is called BRI Shirt Sales, then the data from BRI Shirt Sales up to and including sheet 5 inclusive is copied to the Consolidated sheet? Is that right?
 
Upvote 0
Try this Howard:

VBA Code:
Option Explicit
Sub Howard_2()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, LRowS As Long, LRowD As Long, n As Long, i As Long
    Set ws1 = Worksheets("Consolidated")
    ws1.Range("I:J").EntireColumn.ClearContents
    n = Worksheets("BR1 Shirt Sales").Index
    
    For i = n To Worksheets.Count
        If Worksheets(i).Name <> "Consolidated" Then
            LRowS = Worksheets(i).Range("I:J").Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
            Worksheets(i).Range("I1:J" & LRowS).Copy
            LRowD = ws1.Cells(Rows.Count, "I").End(xlUp).Row
            ws1.Range("I" & LRowD).Offset(1).PasteSpecial xlPasteValues
            Application.CutCopyMode = False
        End If
    Next i
    Application.Goto ws1.Range("A1")
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top