Trying to copy data from one workbook to another (VBA)

Keojey

New Member
Joined
Sep 19, 2018
Messages
35
Office Version
  1. 365
Hello,

I've been racking my brain over why I can't seem to get this to work :

VBA Code:
Sub test()

Dim wbCopy As String, wbDest As String
Dim wsCopy As Worksheet, wsDest As Worksheet
  
  wbCopy = Sheets("test").Range("I4").Value
  wbDest = Sheets("test").Range("I3").Value
  
  Set wsCopy = Workbooks(wbCopy).Worksheets("Report")
  Set wsDest = Workbooks(wbDest).Worksheets("Data")
  
  Workbooks.Open Sheets("test").Range("I2")
  
  wsCopy.Range("B2").Copy _
   wsDest.Range("B1")
  
End Sub
For reference from Sheet "test" : I4's value is Workbook1.xlsm and I3's value is Workbook2.xlsm. I2's value is the path to open Workbook2.xlsm. The code is being ran from a Button located on Workbook1.xlsm.
When running the above code, I get a "Subscript is out of range" error.

If I erase all of the wbCopy and wbDest lines and do this instead, it'll do what I intend for it to do :
VBA Code:
Sub test()

Dim wsCopy As Worksheet, wsDest As Worksheet
  
  Set wsCopy = Workbooks("Workbook1.xlsm").Worksheets("Report")
  Set wsDest = Workbooks("Workbook2.xlsm").Worksheets("Data")
  
  Workbooks.Open Sheets("test").Range("I2")
  
  wsCopy.Range("B2").Copy _
   wsDest.Range("B1")
  
End Sub

But unfortunately the values of what wbCopy and wbDest should equal will have values that will change occasionally in them, hence why I'm trying to get it to work with a string. I just can't seem to find out how to refer to the name of the Workbook properly when I try to Set both wsCopy and wsDest.

Any help is always appreciated!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
....Quick correction. I do NOT have Worksheet2 being opened by this code. I have Sub Test being called from another sub that is opening Worksheet2 before running Sub Test. So in this case the code that is working would like this instead (but is not the solution to the problem) :
VBA Code:
Sub test()

Dim wsCopy As Worksheet, wsDest As Worksheet
 
  Workbooks.Open Sheets("test").Range("I2")
 
  Set wsCopy = Workbooks("Workbook1.xlsm").Worksheets("Report")
  Set wsDest = Workbooks("Workbook2.xlsm").Worksheets("Data")
 
  wsCopy.Range("B2").Copy _
   wsDest.Range("B1")
 
End Sub
 
Last edited:
Upvote 0
Hi Keojey,

not sure but could it be that you get this error because you use the sheet name "test" twice?

wbCopy = Sheets("test").Range("I4").Value
wbDest = Sheets("test").Range("I3").Value
 
Upvote 0
Hi Keojey,

not sure but could it be that you get this error because you use the sheet name "test" twice?

wbCopy = Sheets("test").Range("I4").Value
wbDest = Sheets("test").Range("I3").Value
Unfortunately I played myself while typing this up. I've been busy with other things and haven't had a chance to look at this since I started the post.

I ended up typing it up in a way that worked here and having it wrong on my Excel.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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