VBA Code not running as intended

emack1230

New Member
Joined
Jul 9, 2022
Messages
34
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
We are using VBA to open workbooks, copy and paste into another workbook then close the workbook. When initiating the code we get sub not defined. (Changed file and sheet names for security reasons)
Thank you in advance. (FYI we are learning VBA as we go and wanting to get better)

Here is the code:

Sub OpenWorkbook()

Workbooks.Open "Copy From File"

End Sub

Sub Copy_Method()

Workbooks("Copy From File").Worksheets("Sheet Name").Range("A1:G200").Copy _
Workbooks("Destination File").Worksheets("Sheet Name").Range("A1")

End Sub


Sub CloseWorkbook()


Workbooks("Copy From File").Close SaveChanges:=True


End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello,
What code errors and what line gets highlighted?

Is there a reason you have them in separate sub routines?

Possibly:

VBA Code:
Sub OpenWorkbook()
Dim wb As Workbook, bk As Workbook

Set bk = Workbooks("Destination File.xlsx") 'assumed open
Set wb = Workbooks.Open("CopyFrom.xlsx") 'you would need the full address and name of workbook


wb.Worksheets("Sheet Name").Range("A1:G200").Copy bk.Worksheets("Sheet Name").Range("A1")


wb.Close SaveChanges:=True


End Sub
 
Last edited:
Upvote 0
The error message that pops up is Compile error: Sub or Function not defined. The highlighted line is the very first line Sub OpenWorkbook ()

We are both new to VBA so I wasn't sure about how many times I needed to put in Sub.
 
Upvote 0
It gave me Run-time error '9': Subscript out of range

Highlighted the the line - Set bk - Workbooks(Destination File.xlsx)

To note and not sure if this would matter but both of these workbooks are on Sharepoint and I opened in desktop, went to file and copied the path for each and inserted them into your code where intended.
 
Upvote 0
Subscript out of range means it can't find something.

is Destination File.xlsx opened? There is a comment in the code that it is assumed to be opened.
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,068
Members
449,091
Latest member
remmuS24

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