Copy and paste visible data between 2 selected files

Andy Becker

New Member
Joined
May 17, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, VBA beginner here. I have just started to learn VBA.

I found the following piece of code posted at: Macro prompts User to choose source workbook from folder and paste to destination workbook template

VBA Code:
Sub copyFromPlanillaReservas()

Application.ScreenUpdating = False

   
    Dim cTr As Integer, i As Integer
    Dim FilterType As String, Cap_One As String, TargetFileName As String, SourceFileName As String
    Dim wbTARGET As Workbook, wbSOURCE As Workbook
    Dim ws_TARGET As Worksheet, ws_SOURCE As Worksheet

    FilterType = "Excel Files (*.xls*),*.xls*"
   
    Cap_One = "Select [ SOURCE ] FILE "
    SourceFileName = Application.GetOpenFilename(FilterType, , Cap_One)
    If SourceFileName = "False" Then
        MsgBox "Kindly Locate the SOURCE File location " & _
        vbNewLine & "NO FILE was SELECTED Exiting.....", vbCritical + vbOKOnly, ".."
        Exit Sub
    End If

    Cap_One = "Select [ TARGET ] FILE "
    TargetFileName = Application.GetOpenFilename(FilterType, , Cap_One)
    If TargetFileName = "False" Then
        MsgBox "Kindly Locate the TARGET File location " & _
        vbNewLine & "NO FILE was SELECTED Exiting.....", vbCritical + vbOKOnly, ".."
        Exit Sub
    End If

    Set wbSOURCE = Workbooks.Open(SourceFileName)
    wbSOURCE.Worksheets(1).Activate
    Set ws_SOURCE = wbSOURCE.Worksheets(1)
       
    Set wbTARGET = Workbooks.Open(TargetFileName)
    wbTARGET.Worksheets(1).Activate
    Set ws_TARGET = wbTARGET.Worksheets(1)
    
    
Application.ScreenUpdating = True

End Sub

I do not know how to properly call the source worksheet to copy all data and select the target worksheet to paste all data. I would appreciate your help!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Add this line of code to 'copy all data' of Sheet1 of source file and 'paste all data' in Sheet1 of target file starting from cell A1 (change range in macro as needed).
VBA Code:
    '...
    Set ws_TARGET = wbTARGET.Worksheets(1)
    ws_SOURCE.UsedRange.Copy ws_TARGET.Range("A1") '<- added
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Add this line of code to 'copy all data' of Sheet1 of source file and 'paste all data' in Sheet1 of target file starting from cell A1 (change range in macro as needed).
VBA Code:
    '...
    Set ws_TARGET = wbTARGET.Worksheets(1)
    ws_SOURCE.UsedRange.Copy ws_TARGET.Range("A1") '<- added
    Application.ScreenUpdating = True
End Sub
Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,216,123
Messages
6,128,975
Members
449,480
Latest member
yesitisasport

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