Workbooks.Open Filename error

Kurt

Well-known Member
Joined
Jul 23, 2002
Messages
1,664
I am getting the error Compile error: Syntax error on this line of code:

Set wbSource = Workbooks.Open Filename

the wbSource variable is declared.

What else might be going on?

Code:
Sub GetOpenFName()

    Dim FileName As Variant

    Dim Filt As String, Title As String

    Dim FilterIndex As Integer, Response As Integer

    Dim wbSource As Workbook

    Dim wbDest As Workbook


    Set wbDest = ActiveWorkbook
    

    '   Set to Specified Path\Folder

        On Error Resume Next

        ChDir ActiveWorkbook.Path

    '   Set File Filter

        'Filt = "Excel Files (*.xls, *.xlsx, *.xlsm, *.xlsb), *.xls, *.xlsx, *.xlsm, *.xlsb"

    '   Set *.* to Default

        'FilterIndex = 5

    '   Set Dialogue Box Caption

        Title = "Please select a different File"

    '   Get FileName

        FileName = Application.GetOpenFilename(FileFilter:=Filt, FilterIndex:=FilterIndex, Title:=Title)

    '   Exit if Dialogue box cancelled

        If FileName = False Then

            Response = MsgBox("No File was selected", vbOKOnly & vbCritical, "Selection Error")

            Exit Sub

        End If

    '   Display Full Path & File Name

        Response = MsgBox("You selected " & FileName, vbInformation, "Proceed")

    '   Open Selected Workbook

        Set wbSource = Workbooks.Open FileName
    
 

    'Copy from source workbook to dest workbook

       wbSource.Sheets("RawData").Range("A1:S29089").Copy wbDest.Sheets(“DataCalcs”).Range(“A1”)

 

End Sub
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Even when I add the file name I still get an error.

Set wbSource = Workbooks.Open FileName = "C:\\Documents"RawReports - All of Them - xxxx Copy"
 
Upvote 0
You need to put Filename in brackets
 
Upvote 0
I tried that and I am still getting an error do I just use Documents with Windows 10 now or is there a different syntax for the old My Documents?

Thanks for your first reply.

Set wbSource = Workbooks.Open FileName = "C:\\[Documents"RawReports - All of Them - xxxx Copy]"
 
Upvote 0
This line
Code:
Set wbSource = Workbooks.Open FileName
should be
Code:
Set wbSource = Workbooks.Open (FileName)
 
Upvote 0
This line
Code:
Set wbSource = Workbooks.Open FileName
should be
Code:
Set wbSource = Workbooks.Open (FileName)

This code is starting to work it opens the file dialog box and let's me select the file, but it is still not copying.

What else can I check for?

Code:
Sub GetOpenFName()

    Dim FileName As Variant

    Dim Filt As String, Title As String

    Dim FilterIndex As Integer, Response As Integer

    Dim wbSource As Workbook

    Dim wbDest As Workbook


    Set wbDest = ActiveWorkbook
    

    '   Set to Specified Path\Folder

        On Error Resume Next

        ChDir ActiveWorkbook.Path

    '   Set File Filter

        'Filt = "Excel Files (*.xls, *.xlsx, *.xlsm, *.xlsb), *.xls, *.xlsx, *.xlsm, *.xlsb"

    '   Set *.* to Default

        'FilterIndex = 5

    '   Set Dialogue Box Caption

        Title = "Please select a different File"

    '   Get FileName

        FileName = Application.GetOpenFilename(FileFilter:=Filt, FilterIndex:=FilterIndex, Title:=Title)

    '   Exit if Dialogue box cancelled

        If FileName = False Then

            Response = MsgBox("No File was selected", vbOKOnly & vbCritical, "Selection Error")

            Exit Sub

        End If

    '   Display Full Path & File Name

        Response = MsgBox("You selected " & FileName, vbInformation, "Proceed")

    '   Open Selected Workbook

       Set wbSource = Workbooks.Open("C:\\Documents\RawReports - All of Them - Tommy Copy")
    
 

    'Copy from source workbook to dest workbook

       wbSource.Sheets("RawData").Range("A1:S29089").Copy wbDest.Sheets("Documents\Data Calc Formulas v 1.13b\DataCalcs").Range("A3")

 

End Sub
 
Upvote 0
Firstly, remove this line
Code:
On Error Resume Next
all it does is mask any errors making it very difficult to debug.

Secondly, what is the point of all this code
Code:
    '   Set to Specified Path\Folder

        On Error Resume Next

        ChDir ActiveWorkbook.Path

    '   Set File Filter

        'Filt = "Excel Files (*.xls, *.xlsx, *.xlsm, *.xlsb), *.xls, *.xlsx, *.xlsm, *.xlsb"

    '   Set *.* to Default

        'FilterIndex = 5

    '   Set Dialogue Box Caption

        Title = "Please select a different File"

    '   Get FileName

        FileName = Application.GetOpenFilename(FileFilter:=Filt, FilterIndex:=FilterIndex, Title:=Title)

    '   Exit if Dialogue box cancelled

        If FileName = False Then

            Response = MsgBox("No File was selected", vbOKOnly & vbCritical, "Selection Error")

            Exit Sub

        End If

    '   Display Full Path & File Name

        Response = MsgBox("You selected " & FileName, vbInformation, "Proceed")
if you then go and hardcode the filepath/name into the workbook open?

Finally, this
Code:
Sheets("Documents\Data Calc Formulas v 1.13b\DataCalcs")
is not a valid sheet name
 
Upvote 0
I want the user to select the file first with that name.
 
Last edited by a moderator:
Upvote 0
Please do not quote whole post as it just clutters up the thread.

Remove the on error line & make the change I suggested in post#5
 
Upvote 0
I did that still no luck.

Does this have anything to do with the UNC Universal Naming Convention and file paths on computer networks?
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,854
Members
449,051
Latest member
excelquestion515

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