Hello,
I've been racking my brain over why I can't seem to get this to work :
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 :
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!
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
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!