Error 1004 Workbook Open VBA EXCEL

rodrigo_m_almeida

New Member
Joined
Jan 13, 2022
Messages
42
Office Version
  1. 2021
Platform
  1. Windows
Good afternoon people
Could anyone help me with this?
I'm trying to do if you try to open worbooook that doesn't exist
jump to the next, using my on error goto
VBA Code:
On Error GoTo PULAR
Workbooks.Open Filename:=xPathName & xFileName
...
...
...
PULAR: 
End Sub
Does not work
Continues to display error message
And I can't solve it
ERROR 1004 , EXCEL
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Please show all your code. What line of code is highlighted in debug mode when the error occurs?
 
Upvote 0
Does your "xPathName" variable have a slash at the end of it?

If not, you may need to use:
Rich (BB code):
Workbooks.Open Filename:=xPathName & "\" & xFileName
 
Upvote 0
Please show all your code. What line of code is highlighted in debug mode when the error occurs?
My code is too big
I want to handle this error
This line marked
 

Attachments

  • Captura de tela 2023-02-24 133646.png
    Captura de tela 2023-02-24 133646.png
    10.2 KB · Views: 8
  • Captura de tela 2023-02-24 133710.png
    Captura de tela 2023-02-24 133710.png
    7.2 KB · Views: 7
Upvote 0
Does your "xPathName" variable have a slash at the end of it?

If not, you may need to use:
Rich (BB code):
Workbooks.Open Filename:=xPathName & "\" & xFileName
Yes, there is
The File That Doesn't Exist
On purpose
...
I want to handle this error
Sending to another line I have
to keep running
 
Upvote 0
If you want to ignore the error, just put this line of code before it:
VBA Code:
On Error Resume Next

Just be sure to add this line of code back after it to set it back so you don't accidentally suppress other errors:
VBA Code:
On Error GoTo 0
 
Upvote 0
If you want to ignore the error, just put this line of code before it:
VBA Code:
On Error Resume Next

Just be sure to add this line of code back after it to set it back so you don't accidentally suppress other errors:
VBA Code:
On Error GoTo 0
I tried to do this
But the error message still persists
is being displayed :(

VBA Code:
Private Sub TESTE()
    ' Optimize
        xApplicationOptimize True
    ' Browse Folder 02
        Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
        FileDialog.Title = "Selecione a pasta: FINAL"
        If FileDialog.Show = -1 Then xPathName = FileDialog.SelectedItems(1) Else Exit Sub
        If Right(xPathName, 1) <> "\" Then xPathName = xPathName + "\"
    ' Browse File 01
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .Title = "Selecione o arquivo: BASE_J"
            .InitialFileName = "*.xlsx"
    '  Search File 01
            If .Show() Then xFileName = .SelectedItems(1) Else Exit Sub
        End With
    ' Open File 01
        Workbooks.Open Filename:=xFileName
        Set xOldWB = ActiveWorkbook
        Application.WindowState = xlMinimized
    ' ?
        xL = 2
        Do While Not IsEmpty(xOldWB.Sheets(1).Cells(xL, 1))
NOVAMENTE:
    ' Variables
        xFalse = xOldWB.Sheets(1).Cells(xL, 2)
        xTrue = xOldWB.Sheets(1).Cells(xL, 3)
        xLine = xOldWB.Sheets(1).Cells(xL, 4)
    ' Check_01
        If xTrue = "" Then
            xL = xL + 1
            GoTo PULAR
        End If
        If xOldWB.Sheets(1).Cells(xL, 1) = xOldWB.Sheets(1).Cells(xL - 1, 1) And xTrue <> "" Then GoTo CONTINUA
    ' Search Files 02
        xFileName = "FINAL_" & Format(xOldWB.Sheets(1).Cells(xL, 1), "YYYY_MM_DD") & ".xlsx"
    ' Open File 02
        On Error GoTo PULAR '  DONT WORK <<<
        Workbooks.Open Filename:=xPathName & xFileName ' MY PROBLEM <<< WHEN THE FILE DOES NOT EXIST
        Set xNewWB = ActiveWorkbook
        Application.WindowState = xlMinimized
    ' Change
CONTINUA:
        With ActiveSheet
            .Rows(1).Insert
            .AutoFilte1rMode = False
                With Range("A1", Range("O" & Rows.Count).End(xlUp))
                    .AutoFilter Field:=2, Criteria1:=xFalse
                    .AutoFilter Field:=4, Criteria1:=xLine
                     Range("B1", Range("B" & Rows.Count).End(xlUp)) = xTrue
                End With
    ' Disregard
            .AutoFilterMode = False
                With Range("B1", Range("B" & Rows.Count).End(xlUp))
                    .AutoFilter 1, "N/A"
                    .Offset(1).SpecialCells(12).EntireRow.Delete
                End With
            .AutoFilterMode = False
            .Rows(1).Delete
        End With
    ' Next file
        xL = xL + 1
    ' Check_02
        If xOldWB.Sheets(1).Cells(xL, 1) = xOldWB.Sheets(1).Cells(xL - 1, 1) And xTrue <> "" Then GoTo NOVAMENTE
    ' Save File 02
        xNewWB.Save
    ' Close File 02
        xNewWB.Close
PULAR:
        Loop
    ' Save File 01
        'xOldWB.Save
    ' Close File 01
        xOldWB.Close
    ' Optimize
        xApplicationOptimize False
End Sub

This is my full code
 
Last edited:
Upvote 0
I tried to do this
But the error message still persists
is being displayed :(
Works fine for me when I added those lines, but then I don't know what the rest of your code is doing.
Without seeing the rest of your code, I am afraid it is going to be very difficult to help you.
 
Upvote 0
Works fine for me when I added those lines, but then I don't know what the rest of your code is doing.
Without seeing the rest of your code, I am afraid it is going to be very difficult to help you.
it worked that way
That said,
Problem is that I was debugging on f8... haha

It just didn't solve
Why can't I let the code continue to run
needed to jump to another line,
when it gives error (use the 'GoTo')
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,313
Members
453,032
Latest member
Pauh

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