0

I'm trying to create a macro in Excel that allow me to copy and paste data from one workbook to another workbook. The workbook names are always going to be different, so I used GetOpenFilename() method and stored it in a variable, so I can specifically choose the workbook I want. However, I am getting a runtime 9 error.

Here's the code so far. (macro1 is just another macro I created that crunches numbers)

Sub Everything()

Dim f1 As String 
Dim f2 As String 
Dim wb1 As Workbook
Dim wb2 As Workbook

f1 = Application.GetOpenFilename(FileFilter:="Excel Files,*xl*;*xm*;")
Set wb1 = Workbooks.Open(f1)

f2 = Application.GetOpenFilename(FileFilter:="Excel Files,*xl*;*xm*;")
Set wb2 = Workbooks.Open(f2)

Call Macro1

Workbooks(wb2.Name).Worksheets("Sheet1").Range("D4:D25").Copy _
Workbooks(wb1.Name).Worksheets("Sheet1").Range("E11:E32")

End Sub

I get the run time error on the very last two lines of code. Please help, thank you!

  • Lots of questions asking about this type of this. Have a look at my question and the answers here: https://stackoverflow.com/q/30575923/4961700 Vote if you find it helpful. – Solar Mike Nov 07 '20 at 08:18
  • Are you sure that the error number is 9 and not 91? – VBasic2008 Nov 07 '20 at 09:53
  • Note that as you've already created wb1 and wb2 as workbook objects, you don't need `Workbooks(wb2.Name)`. You should be able to just write `wb2.Worksheets("Sheet1").Range("D4:D25").Copy wb1.Worksheets("Sheet1").Range("E11")`. What happens if you try `wb1.Worksheets("Sheet1").Range("E11:E32").Value = wb2.Worksheets("Sheet1").Range("D4:D25").Value` as an alternative? – user9601310 Nov 07 '20 at 10:14

0 Answers0