Need macro to copy from another book

Russk68

Well-known Member
Joined
May 1, 2006
Messages
589
Office Version
  1. 365
Platform
  1. MacOS
Hi
Using a macro, I need to copy column A from a sheet in another book in the same folder as my current book and paste to column A. I would like not to have to open the book to do this. I assume that the book name and sheet name would have to always be the same; such as Book1, Sheet1. I would then create a button to assign the macro to.
TIA!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Should be something like this
VBA Code:
Sub ImportData()

Dim FName As String, FPath As String
Dim strColMaster As String, strColSource As String
Dim rngCopy As Range
Dim wsMaster As Worksheet, wsSource As Worksheet
Dim wbMaster As Workbook, wbSource As Workbook

Set wbMaster = ActiveWorkbook
Set wsMaster = wbMaster.Sheets("Sheet1")                  ' Change Master sheet name as reuired

FPath = wbMaster.Path
FName = "Book1.xlsx"                                                 ' Put the Source file name here

Application.ScreenUpdating = False

Set wbSource = Workbooks.Open(Filename:=FPath & "\" & FName, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
Set wsSource = wbSource.Sheets("Sheet1")                    ' Change Source sheet name as required

Set rngCopy = wsSource.Range("A1", wsSource.Cells(Rows.Count, "A").End(xlUp))

rngCopy.Copy wsMaster.Range("A1")                           ' Paste copied range to A1 in wsMaster

wbSource.Close False                                                  ' Close wbSource without saving

End Sub
 
Upvote 0
Hi Zot
I'm getting this error. The path looks good to me.
Thank you for your help!
 

Attachments

  • Screen Shot 2021-10-22 at 11.48.02 AM.png
    Screen Shot 2021-10-22 at 11.48.02 AM.png
    122.4 KB · Views: 7
  • Screen Shot 2021-10-22 at 11.48.18 AM.png
    Screen Shot 2021-10-22 at 11.48.18 AM.png
    106.4 KB · Views: 7
  • Screen Shot 2021-10-22 at 11.49.08 AM.png
    Screen Shot 2021-10-22 at 11.49.08 AM.png
    117.8 KB · Views: 7
Upvote 0
More clear images
 

Attachments

  • Screen Shot 2021-10-22 at 11.58.56 AM.png
    Screen Shot 2021-10-22 at 11.58.56 AM.png
    132.4 KB · Views: 2
  • Screen Shot 2021-10-22 at 12.00.35 PM.png
    Screen Shot 2021-10-22 at 12.00.35 PM.png
    187.1 KB · Views: 2
Upvote 0
I think backslash mistake here
Set wbSource = Workbooks.Open(Filename:=FPath & "\" & FName, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
 
Upvote 0
I changed it to "/" and I don't get an error now. I see something happen but column A remains blank.
Heres. a screenshot from Book1, Sheet1.
Unfortunately I have to reduce the file size to upload and now it's unreadable.
Column A has information
 

Attachments

  • Screen Shot 2021-10-22 at 12.44.53 PM.png
    Screen Shot 2021-10-22 at 12.44.53 PM.png
    94.5 KB · Views: 9
Upvote 0
I have tested the code with mock-up source Book1.xlsx and no problem to copy data.

If everything is correct like workbook name and sheet name in the source and the import destination sheet name is also Sheet1, then I have no idea. I could hardly see the screenshot, even with microscope ?

I'm not sure if it is something to do with Mac. I just realized that you are on Mac. :unsure:
 
Upvote 0
I have tested the code with mock-up source Book1.xlsx and no problem to copy data.

If everything is correct like workbook name and sheet name in the source and the import destination sheet name is also Sheet1, then I have no idea. I could hardly see the screenshot, even with microscope ?

I'm not sure if it is something to do with Mac. I just realized that you are on Mac. :unsure:
I'll try it on Windows and let you know. Thanks for your time!
 
Upvote 0
I thought the goal was to not open the workbook?
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,652
Members
448,975
Latest member
sweeberry

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