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
64
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
 

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Mentor82

Active Member
Joined
Dec 30, 2018
Messages
311
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
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,
 

Harshil Mehta

Board Regular
Joined
May 14, 2020
Messages
64
Office Version
  1. 2013
Platform
  1. Windows
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.
 

Mentor82

Active Member
Joined
Dec 30, 2018
Messages
311
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
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
 

Mentor82

Active Member
Joined
Dec 30, 2018
Messages
311
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
Pleasure is mine :).
Thanks for feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,035
Messages
5,622,332
Members
415,894
Latest member
silverhaze

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
Top