Copy data from another workbook (user's choice) till last cell of the row to a specified column

Harshil Mehta

Board Regular
Joined
May 14, 2020
Messages
85
Office Version
  1. 2013
Platform
  1. Windows
Sub Get_Data_From_Files()

Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim lr As Long
lr = Cells(Rows.Count, "B").End(xlUp).Rows
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)
OpenBook.Sheets(3).RANGE("B2:AK" & lr).Copy
ThisWorkbook.Worksheets("Input Data").RANGE("C3").PasteSpecial xlPasteValues
OpenBook.Close False

End If
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi,
Would you mind explaining what’s the problem you face and what do you expect as a result? I’m asking because you did not described your issue but for having pasted your code.

thx,
 
Upvote 0
Hi,
Would you mind explaining what’s the problem you face and what do you expect as a result? I’m asking because you did not described your issue but for having pasted your code.

thx,
I want to copy the data from another workbook's sheet 3, B2 (last value in the row) till column AK.
APPLICATION-DEFINED OR OBJECT-DEFINED ERROR is displayed when I run the code. Also, the highlighting the line OpenBook.Sheets(3).RANGE("B2:AK" & lr).Copy when clicked on debug.
 
Upvote 0
Hi,
Thanks a lot for the reply.
Here's the fixed code. Check this out.

VBA Code:
Sub Get_Data_From_Files()

Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim lr 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)
lr = OpenBook.Worksheets(3).Cells(Rows.Count, "B").End(xlUp).Row
OpenBook.Sheets(3).Range("B2:AK" & lr).Copy ThisWorkbook.Worksheets("Input Data").Range("C3")
OpenBook.Close False

End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Pleasure is mine :).
Thanks for feedback.
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,162
Members
448,554
Latest member
Gleisner2

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