[VBA] Run-time error '1004'

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi team,

this is the error I am randomly getting while opening my Excel file with macros (.xlm).

1676623491643.png



This is the opening procedure.

VBA Code:
Private Sub Workbook_Open()
With Application
.ScreenUpdating = False: .WindowState = xlMaximized: .DisplayStatusBar = True

Call OptimizeCode_Begin

Worksheets("WELCOME").Activate
With ActiveWindow
Worksheets("WELCOME").Range("A1:AD1").Select
.DisplayHorizontalScrollBar = False: .DisplayVerticalScrollBar = False: .DisplayWorkbookTabs = False: .ZOOM = True: .ScrollRow = 1: .ScrollColumn = 1
Worksheets("WELCOME").Range("A1").Activate
End With

Call OptimizeCode_End

.ScreenUpdating = True
End With

End Sub

Here is the macro OptimizeCode_Begin & OptimizeCode_End.

VBA Code:
Sub OptimizeCode_Begin()
EventState = Application.EnableEvents
Application.EnableEvents = False
CalcState = Application.Calculation
Application.Calculation = xlCalculationManual
Application.DisplayFormulaBar = False
PageBreakState = ActiveSheet.DisplayPageBreaks
ActiveSheet.DisplayPageBreaks = False
End Sub


Sub OptimizeCode_End()
ActiveSheet.DisplayPageBreaks = PageBreakState
With Application
.Calculation = CalcState: .Calculation = xlCalculationAutomatic: .EnableEvents = EventState: .EnableEvents = True
End With
End Sub

What could cause this error?

TY!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I would recommend copying your "Workbook_Open" procedure code and pasting it in a manual VBA code block, like this:
VBA Code:
Sub Test()

With Application
.ScreenUpdating = False: .WindowState = xlMaximized: .DisplayStatusBar = True

Call OptimizeCode_Begin

Worksheets("WELCOME").Activate
With ActiveWindow
Worksheets("WELCOME").Range("A1:AD1").Select
.DisplayHorizontalScrollBar = False: .DisplayVerticalScrollBar = False: .DisplayWorkbookTabs = False: .ZOOM = True: .ScrollRow = 1: .ScrollColumn = 1
Worksheets("WELCOME").Range("A1").Activate
End With

Call OptimizeCode_End

.ScreenUpdating = True
End With

End With
Then, manually run this code by stepping through it one line at a time using the F8 key, and see which line of code the error occurs on.
That should give you a good idea of where exactly the error is occurring, and what line of code to focus your attention on.
Often times the error becomes evident when you see exactly what is happening (i.e. maybe the worksheet is hidden, range is protected, etc).
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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