VBA syntax error when trying to copy data from another workbook

Saab95

New Member
Joined
Mar 26, 2021
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm getting a syntax error when trying to run this. What I'm doing is:
I have workbook open, then when I run vba it asks me to open another workbook, from which it copies from B4 until E last row in worksheet called balances.
Then it pastes data in current workbook from A last row.

A bit of help would be appreciated as I'm a beginner in vba. Thanks

Sub Get_Data_From_File()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim ws As Worksheet
Dim lastRow As Long
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
lastRow = OpenBook.Balance.Range("E" & Rows.Count).End(xlUp).Row
OpenBook.Balance.Range("B4:E" & lastRow").Copy
lastRow = ThisWorkbook.Worksheets("Import Data").Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Worksheets("Import Data").Range("E" & Rows.Count").PasteSpecial xlPasteValues
OpenBook.Close False

Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Code:
OpenBook.("Balance").Range("B4:E" & lastRow).Copy
lastRow = ThisWorkbook.Worksheets("Import Data").Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Worksheets("Import Data").Range("E" & Rows.Count).PasteSpecial xlPasteValues
HTH. Dave
 
Upvote 0
What is Balance? It has been declared or assigned to anything.
 
Upvote 0
In that case you need to use
VBA Code:
lastRow = OpenBook.Sheets("Balance").Range("E" & Rows.Count).End(xlUp).Row
 
Upvote 0
I have changed the code but now experiencing a different problem

1668441987854.png

1668441997882.png
 
Upvote 0
Solved.

Sub Get_Data_From_File()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim ws As Worksheet
Dim lastRow As Long
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
lastRow = OpenBook.Worksheets("Balance").Range("E" & Rows.Count).End(xlUp).Row
OpenBook.Worksheets("Balance").Range("B4:E" & lastRow).Copy
lastRow = ThisWorkbook.Worksheets("Input_Rec_Report").Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Worksheets("Input_Rec_Report").Range("A" & lastRow).PasteSpecial xlPasteValues
OpenBook.Close False

Application.ScreenUpdating = True
End If
End Sub
 
Upvote 0
Solution
Glad you sorted it & thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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