Import Data from another Workbook

tigerdel

Board Regular
Joined
Oct 13, 2015
Messages
145
Office Version
  1. 365
Platform
  1. Windows
I have the code below, but it throws up an Object Required error at the line: Set OpenBook = FileToOpen

VBA Code:
Sub Data()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim FileName As String
Application.WindowState = xlMaximized
Application.ScreenUpdating = False
ChDir Environ$("userprofile") & "\OneDrive\MJM Services\2.0 Quotations\xlsm Copy\"
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File", FileFilter:="Excel Files(*.xls*),*.xls*")
Workbooks.Open FileName:=FileToOpen
Set OpenBook = FileToOpen
If FileToOpen <> False Then
With OpenBook.Sheet1.Activate
.Range("A1:K500").Select
End With
Application.CutCopyMode = False
Selection.Copy
Workbooks("Quotations.xlsm").Sheets("Sheet1").Activate
Sheet5.Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Range("A1").Paste
Range("A1").Select
Application.CutCopyMode = False
Cells.Select
Cells.EntireColumn.AutoFit
OpenBook.Close False
Application.ScreenUpdating = True
End If
End Sub

Any ideas what I am doing wrong here?

Thanks for looking
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
try this update to your code & see if helps

VBA Code:
FileToOpen = Application.GetOpenFilename(Title:="Browse For your File", FileFilter:="Excel Files(*.xls*),*.xls*")
    If FileToOpen <> False Then
     Set OpenBook = Workbooks.Open(FileName:=FileToOpen)
     
     'rest of code

Dave
 
Upvote 0
@tigerdel, you are trying to assign a String variable to an Object variable resulting in the mentioned error message. I would like to note that this line: With OpenBook.Sheet1.Activate also would return an error. Just like this one: Sheet5.Select because selecting worksheets by their codename is not allowed. In addition to that it's worth mentioning, that it is almost never necessary to select on worksheets or ranges with the Select method.
As an alternative to Dave's suggestion some code below. I would advise you to make copies of the affected workbooks before trying this out.

VBA Code:
Sub Data_r2()
    
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Dim FileName As String
    
    Dim oWbTarget   As Workbook
    
    Application.WindowState = xlMaximized
    Application.ScreenUpdating = False
    ChDir Environ$("userprofile") & "\OneDrive\MJM Services\2.0 Quotations\xlsm Copy\"
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File", FileFilter:="Excel Files(*.xls*),*.xls*")
    
    On Error Resume Next
    Set OpenBook = Workbooks.Open(FileName:=FileToOpen)
    On Error GoTo 0
    
    If Not OpenBook Is Nothing Then
    
        Set oWbTarget = Workbooks("Quotations.xlsm")

        With oWbTarget.Sheets("Sheet5")
            .Cells.Delete Shift:=xlUp
            OpenBook.Sheets("Sheet1").Range("A1:K500").Copy Destination:=.Range("A1")
            .Cells.EntireColumn.AutoFit
        End With
        OpenBook.Close False
    End If
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
You are welcome and thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,090
Members
448,944
Latest member
sharmarick

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