VBA to Copy from one workbook to another workbook

gleamng

Board Regular
Joined
Oct 8, 2016
Messages
98
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Good day everyone
I need help on how to copy from a workbook to another workbook. For instance i want to copy from column D5 to last row from sheet named INDEX from workbook General.xlsm to column G7 downward, sheet named"Pearl" in workbook named "summary.xlsm".

Thank you for your help always and have a great day all.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Give this a try on a copy of your worbook.
It assumes you have both workbooks open.

I have the "copy values" line as active but have provided a commented out section if you want to copy values and formats.
For the latter comment out the sumRng.Resize(genRng.Rows.Count).Value = genRng.Value and uncomment the 4 lines in the Copy values and formats section.

VBA Code:
Sub CopyData()

    Dim wbSum As Workbook, wbGen As Workbook
    Dim shtSum As Worksheet, shtGen As Worksheet
    Dim sumLastRow As Long, genLastRow As Long
    Dim sumRng As Range, genRng As Range
    
    Application.ScreenUpdating = False
    
    Set wbSum = Workbooks("summary.xlsm")
    Set wbGen = Workbooks("General.xlsm")
    
    Set shtSum = wbSum.Worksheets("Pearl")
    Set sumRng = shtSum.Range("G7")
 
    Set shtGen = wbGen.Worksheets("INDEX")
    With shtGen
        genLastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
        If genLastRow < 5 Then
            MsgBox "No data to copy in Column D"
            Exit Sub
        End If
        Set genRng = .Range(.Cells(5, "D"), .Cells(genLastRow, "D"))
    End With
    
    ' Copy values only
    sumRng.Resize(genRng.Rows.Count).Value = genRng.Value
    
    ' --- Alternative copy & paste method ---
    ' Copy values and formats
'    genRng.Copy
'    sumRng.PasteSpecial Paste:=xlPasteValues
'    sumRng.PasteSpecial Paste:=xlPasteFormats
'    Application.CutCopyMode = False
    ' --- end copy paste ---
    
    sumRng.EntireColumn.AutoFit

    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Thanks a million, it worked perfectly as wanted. God bless you richly.
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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