Copy specific Columns from one workbook to Another - VBA Excel

josephmary

New Member
Joined
Oct 18, 2023
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Need help in adding a macro to copy specific columns from one workbook to another. Just that part of the code where the copy and pasting happens. See image below. Thank you

1698658497192.png
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I have one more question,

What happens if Sheet1 has 20 records for "13-May" and Sheet2 has 15 records for "13-May"?
Which ones must be chosen?
 
Upvote 0
I have one more question,

What happens if Sheet1 has 20 records for "13-May" and Sheet2 has 15 records for "13-May"?
Which ones must be chosen?
apologies i wasn't clear enough. i just want everything to be overwritten on sheet 2
 
Upvote 0
This must work for you

VBA Code:
Sub test()
  Dim columnG As Variant, columnOR As Variant, columnZ As Variant
  With Worksheets("Sheet1")
  columnG = Intersect(.UsedRange, .Range("G:G"))
  columnOR = Intersect(.UsedRange, .Range("O:R"))
  columnZ = Intersect(.UsedRange, .Range("Z:Z"))
  End With
  
  With Worksheets("Sheet2")
  Intersect(.UsedRange, .Range("C:I")).ClearContents
  .Range("C2").Resize(UBound(columnG, 1), 1).Value = columnG
  .Range("D2").Resize(UBound(columnOR, 1), UBound(columnOR, 2)).Value = columnOR
  .Range("I2").Resize(UBound(columnZ, 1), 1).Value = columnZ
  End With
End Sub
 
Upvote 0
Thank you. I just added some other codes as I decided to copy it from another workbook
 
Upvote 0
Then something like this may work. But it needs both workbooks to be open:
VBA Code:
Sub test()
  Dim columnG As Variant, columnOR As Variant, columnZ As Variant
  With Workbooks("SourceWorkbook.xlsx")
  .Active
  With .Worksheets("Sheet1")
  .Active
  columnG = Intersect(.UsedRange, .Range("G:G"))
  columnOR = Intersect(.UsedRange, .Range("O:R"))
  columnZ = Intersect(.UsedRange, .Range("Z:Z"))
  End With
  End With
 
  With Workbooks("DestinationWorkbook.xlsx")
  .Active
  With .Worksheets("Sheet1")
  Intersect(.UsedRange, .Range("C:I")).ClearContents
  .Range("C2").Resize(UBound(columnG, 1), 1).Value = columnG
  .Range("D2").Resize(UBound(columnOR, 1), UBound(columnOR, 2)).Value = columnOR
  .Range("I2").Resize(UBound(columnZ, 1), 1).Value = columnZ
  End With
  End With
End Sub
Please pay attention to file extensions. You have to specify either .xlsx or .xlsm
 
Upvote 0
Solution
Then something like this may work. But it needs both workbooks to be open:
VBA Code:
Sub test()
  Dim columnG As Variant, columnOR As Variant, columnZ As Variant
  With Workbooks("SourceWorkbook.xlsx")
  .Active
  With .Worksheets("Sheet1")
  .Active
  columnG = Intersect(.UsedRange, .Range("G:G"))
  columnOR = Intersect(.UsedRange, .Range("O:R"))
  columnZ = Intersect(.UsedRange, .Range("Z:Z"))
  End With
  End With
 
  With Workbooks("DestinationWorkbook.xlsx")
  .Active
  With .Worksheets("Sheet1")
  Intersect(.UsedRange, .Range("C:I")).ClearContents
  .Range("C2").Resize(UBound(columnG, 1), 1).Value = columnG
  .Range("D2").Resize(UBound(columnOR, 1), UBound(columnOR, 2)).Value = columnOR
  .Range("I2").Resize(UBound(columnZ, 1), 1).Value = columnZ
  End With
  End With
End Sub
Please pay attention to file extensions. You have to specify either .xlsx or .xlsm
Thanks so much! appreciate it!
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,147
Members
449,098
Latest member
Doanvanhieu

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