Combine ranges in 3 different sheets

Dokat

Active Member
Joined
Jan 19, 2015
Messages
302
Office Version
  1. 365
Hi,

I have a workbook with three worksheets "Dairy", "Milk" and "Cream". Ranges in each work sheet is

Dairy: A2:BO last row with data
Milk: A5:BO last row with data
Cream: A5:Y last row with data

I am trying to combine 3 worksheets copy paste under sheet "Farm".
I would like to copy the Dairy Range first, then copy Milk range below last row of Dairy Range and copy Cream range under last row of Milk Range. Is this possible to do with VBA. Appreciate any help. Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
VBA Code:
Option Compare Text                                             'make module case insensitive

Sub Farm()
     For Each shname In Array("dairy", "milk", "cream")         'loop through you 3 sheets
          With Sheets(shname)                                   ' a sheet
               Set c = .Range("A" & IIf(shname = "dairy", 2, 5))     'topleft cell
               rij = .Range("A" & .Rows.Count).End(xlUp).Row    'last row
               If rij >= c.Row Then c.Resize(rij - c.Row + 1, .Columns("BO").Column).Copy Sheets("farm").Range("a" & Rows.Count).End(xlUp).Offset(1)
          End With
     Next
End Sub
 
Upvote 0
VBA Code:
Option Compare Text                                             'make module case insensitive

Sub Farm()
     For Each shname In Array("dairy", "milk", "cream")         'loop through you 3 sheets
          With Sheets(shname)                                   ' a sheet
               Set c = .Range("A" & IIf(shname = "dairy", 2, 5))     'topleft cell
               rij = .Range("A" & .Rows.Count).End(xlUp).Row    'last row
               If rij >= c.Row Then c.Resize(rij - c.Row + 1, .Columns("BO").Column).Copy Sheets("farm").Range("a" & Rows.Count).End(xlUp).Offset(1)
          End With
     Next
End Sub
Thank you for your response. Code works however it copy pastes starting row 5. Anyway to copy paste starting row 2?
 
Upvote 0
Dairy: A2:BO last row with data
Milk: A5:BO last row with data
Cream: A5:Y last row with data
so dairy is 2 and the rest is 5, isn't it ?
VBA Code:
 Set c = .Range("A" & IIf(shname = "dairy", 2, 5))     'topleft cell
 
Upvote 0
so dairy is 2 and the rest is 5, isn't it ?
VBA Code:
 Set c = .Range("A" & IIf(shname = "dairy", 2, 5))     'topleft cell
Thanks for your reply. I figured THE issue was there was already content in row 3 worksheet "Farm" that's why it paste starting row 5. Once i clear the content and rerun it works correctly
 
Upvote 0

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

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