Trying to combine code on ThisWorkbook to change all worksheets contained

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
Compile error: Variable not defined

VBA Code:
             x = ActiveSheet.Name

I can't see where I have gone wrong.

VBA Code:
Private Sub Workbook_SheetActivate(ByVal wks As Object)
            With Application
             x = ActiveSheet.Name
            For Each wks In Worksheets
            Sheets(wks.Name).Select
            ActiveWindow.DisplayHeadings = False
            ActiveWindow.DisplayHorizontalScrollBar = False
            ActiveWindow.DisplayVerticalScrollBar = True
            .DisplayFullScreen = True
            .DisplayFormulaBar = False
            .DisplayStatusBar = False
            .CommandBars("Full Screen").Visible = False
            .CommandBars("Worksheet Menu Bar").Enabled = False
            .CommandBars("Standard").Visible = False
            .CommandBars("Formatting").Visible = False
            Next wks
            Sheets(x).Select
            End With
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Compile error: Variable not defined

VBA Code:
             x = ActiveSheet.Name

I can't see where I have gone wrong.

VBA Code:
Private Sub Workbook_SheetActivate(ByVal wks As Object)
            With Application
             x = ActiveSheet.Name
            For Each wks In Worksheets
            Sheets(wks.Name).Select
            ActiveWindow.DisplayHeadings = False
            ActiveWindow.DisplayHorizontalScrollBar = False
            ActiveWindow.DisplayVerticalScrollBar = True
            .DisplayFullScreen = True
            .DisplayFormulaBar = False
            .DisplayStatusBar = False
            .CommandBars("Full Screen").Visible = False
            .CommandBars("Worksheet Menu Bar").Enabled = False
            .CommandBars("Standard").Visible = False
            .CommandBars("Formatting").Visible = False
            Next wks
            Sheets(x).Select
            End With
End Sub
Need to declare the x variable

Try this:

VBA Code:
Private Sub Workbook_SheetActivate(ByVal wks As Object)

Dim x As String

            With Application

             x = ActiveSheet.Name

            For Each wks In Worksheets
            
                      Sheets(wks.Name).Select
          
                      ActiveWindow.DisplayHeadings = False
          
                      ActiveWindow.DisplayHorizontalScrollBar = False
          
                      ActiveWindow.DisplayVerticalScrollBar = True
          
                      .DisplayFullScreen = True
          
                      .DisplayFormulaBar = False
          
                      .DisplayStatusBar = False
          
                      .CommandBars("Full Screen").Visible = False
          
                      .CommandBars("Worksheet Menu Bar").Enabled = False
          
                      .CommandBars("Standard").Visible = False
          
                      .CommandBars("Formatting").Visible = False
            Next wks

            Sheets(x).Select

            End With

End Sub
 
Upvote 0
Need to declare the x variable

Try this:

VBA Code:
Private Sub Workbook_SheetActivate(ByVal wks As Object)

Dim x As String

            With Application

             x = ActiveSheet.Name

            For Each wks In Worksheets
           
                      Sheets(wks.Name).Select
         
                      ActiveWindow.DisplayHeadings = False
         
                      ActiveWindow.DisplayHorizontalScrollBar = False
         
                      ActiveWindow.DisplayVerticalScrollBar = True
         
                      .DisplayFullScreen = True
         
                      .DisplayFormulaBar = False
         
                      .DisplayStatusBar = False
         
                      .CommandBars("Full Screen").Visible = False
         
                      .CommandBars("Worksheet Menu Bar").Enabled = False
         
                      .CommandBars("Standard").Visible = False
         
                      .CommandBars("Formatting").Visible = False
            Next wks

            Sheets(x).Select

            End With

End Sub
Thank you for your reply. The code appears to be fine until executed, then it created an infinite loop and I can't understand why? There are three worksheets, so why doesn't it update the three and then stop?
 
Upvote 0
Endless loop
VBA Code:
Private Sub Workbook_SheetActivate(ByVal wks As Object)
    Application.ScreenUpdating = False
        Dim x As String
            With Application
                x = ActiveSheet.Name
                    For Each wks In Worksheets
                        Sheets(wks.Name).Select
                            ActiveWindow.DisplayHeadings = False
                            ActiveWindow.DisplayHorizontalScrollBar = False
                            ActiveWindow.DisplayVerticalScrollBar = True
                            .DisplayFullScreen = True
                            .DisplayFormulaBar = False
                            .DisplayStatusBar = False
                            .CommandBars("Full Screen").Visible = False
                            .CommandBars("Worksheet Menu Bar").Enabled = False
                            .CommandBars("Standard").Visible = False
                            .CommandBars("Formatting").Visible = False
                    Next wks
                            Sheets(x).Select
            End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Endless loop
VBA Code:
Private Sub Workbook_SheetActivate(ByVal wks As Object)
    Application.ScreenUpdating = False
        Dim x As String
            With Application
                x = ActiveSheet.Name
                    For Each wks In Worksheets
                        Sheets(wks.Name).Select
                            ActiveWindow.DisplayHeadings = False
                            ActiveWindow.DisplayHorizontalScrollBar = False
                            ActiveWindow.DisplayVerticalScrollBar = True
                            .DisplayFullScreen = True
                            .DisplayFormulaBar = False
                            .DisplayStatusBar = False
                            .CommandBars("Full Screen").Visible = False
                            .CommandBars("Worksheet Menu Bar").Enabled = False
                            .CommandBars("Standard").Visible = False
                            .CommandBars("Formatting").Visible = False
                    Next wks
                            Sheets(x).Select
            End With
    Application.ScreenUpdating = True
End Sub

It may be the Sheets(wks.Name).Select, which I'm not sure what that's attempting to do. It looks like that's attempting to select the active sheet in the loop, which wouldn't be necessary as it's already the activesheet. From my testing, when I exclude this part, there's no longer any endless loop.

Also, other changes I made was to the ByVal Sh As object, which is the default when I select that from the workbook declarations drop down. I then declared wks as a worksheet.

VBA Code:
Option Explicit
Private Sub Workbook_SheetActivate(ByVal Sh As Object)

Application.ScreenUpdating = False
Dim x As String, wks As Worksheet

With Application
    x = ActiveSheet.Name
        For Each wks In Worksheets
            'Sheets(wks.Name).Select
            ActiveWindow.DisplayHeadings = False
            ActiveWindow.DisplayHorizontalScrollBar = False
            ActiveWindow.DisplayVerticalScrollBar = True
            .DisplayFullScreen = True
            .DisplayFormulaBar = False
            .DisplayStatusBar = False
            .CommandBars("Full Screen").Visible = False
            .CommandBars("Worksheet Menu Bar").Enabled = False
            .CommandBars("Standard").Visible = False
            .CommandBars("Formatting").Visible = False
        Next wks
    Sheets(x).Select
End With
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Thank you very much. It didn't need much changing, but enough to make all the difference. It was good that you also declared wks in the beginning to make it even shorter.
 
Upvote 0

Forum statistics

Threads
1,215,999
Messages
6,128,186
Members
449,431
Latest member
Taekwon

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