I have two excel documents. I'm trying to write a VBA code to simply copy a bunch of cells from the first workbook to the second workbook. The following code works when it is in a VBA code module, and I run the code by pressing F8. This Macro2()
is in the General option.
Sub Macro2()
Workbooks("Feeder document for quoting.xlsm").Worksheets("Factor").Range("A4:AE34").Copy _
Destination:=Workbooks("Cost-Sell Sheet.xlsm").Worksheets("RFJN").Range("A4:AE34")
End Sub
But when I call this macro from another macro, I get "Application-defined or object-defined error", which I don't fully understand. This other macro is triggered by a command button.
I have two excel documents. I'm trying to write a VBA code to simply copy a bunch of cells from the first workbook to the second workbook. The following code works when it is in a VBA code module, and I run the code by pressing F8. This Macro2()
is in the General option.
Sub Macro2()
Workbooks("Feeder document for quoting.xlsm").Worksheets("Factor").Range("A4:AE34").Copy _
Destination:=Workbooks("Cost-Sell Sheet.xlsm").Worksheets("RFJN").Range("A4:AE34")
End Sub
But when I call this macro from another macro, I get "Application-defined or object-defined error", which I don't fully understand. This other macro is triggered by a command button.
Share Improve this question edited Mar 25 at 16:53 CDP1802 16.5k2 gold badges10 silver badges18 bronze badges asked Mar 25 at 16:05 ham famham fam 13 bronze badges 4 |1 Answer
Reset to default 0You didn't have lines to open either workbooks
Try to have some line like:
workbook.open (Your Workbook Location)
---------------------------------------------------
EDIT: below
Sub CopyB()
Dim WB0 As Workbook
Set WB0 = ThisWorkbook
Dim WB1 As Workbook
Set WB1 = Workbooks.Open("E:\DESKTOP\Import\b1.xlsx")
Dim WS0 As Worksheet
Set WS0 = WB0.Sheets("ABC")
Dim RNG As Range
Set RNG = WS0.Range("A1:B2")
RNG.Copy Destination:=WB1.Sheets(1).Range("A1")
End Sub
Dim WbSource as Workbook: Set WbSource = Workbooks("Feeder document for quoting.xlsm")
andDim WbDestination as Workbook: Set WbDestination = Workbooks("Cost-Sell Sheet.xlsm")
(fix with the filepaths) see here – Oran G. Utan Commented Mar 25 at 17:06Workbook
variables if those files are open, right? – BigBen Commented Mar 25 at 18:41