Sheets names flashing up in duplicate when running vba

Kerryx

Well-known Member
Joined
May 6, 2016
Messages
766
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
1741600473960.png

When running my VBA code the sheet names flash up in duplicate at the bottom of the screen, any thoughts why.


VBA Code:
Sub Optimise(Flag As Boolean)
On Error Resume Next
F = Not Flag
Application.ScreenUpdating = F
Application.DisplayAlerts = F
Application.EnableEvents = F
Application.DisplayStatusBar = F
ActiveSheet.DisplayPageBreaks = F
If F = True Then
Application.Calculation = xlCalculationAutomatic
Else
Application.Calculation = xlCalculationManual
End If
On Error GoTo 0
End Sub

Sub Update()

'switch off updating to stop screen flickering

Optimise (True)

SheetList = Array("Leaderboard", "Entries_by_Name", "Points_by_League", "Tables", "Admin")


For Count = 0 To UBound(SheetList)

'Unprotect the sheets
Sheets(SheetList(Count)).Unprotect

'Add date and time to admin sheet
Sheets("Admin").Range("A2").Value = Now

Next


' Update - Refresh Queries
'
ActiveWorkbook.RefreshAll
DoEvents


' Sort the sheet

With ActiveWorkbook.Worksheets("Leaderboard").Sort
.SortFields.Clear
.SortFields.Add Range("P1"), , xlAscending 'Sort by Sort Help Help column P
.SortFields.Add Range("R1"), , xlDescending ' Sort by GD value
.SortFields.Add Range("D1"), , xlDescending 'Sort Total column

.SetRange Range("B1:R350")
.Header = xlYes

.Apply
End With
Sheets("Leaderboard").Select
Range("A1").Select

'Allow screen to update now

Optimise (False)

'Reprotect the modified sheets

For Count = 0 To UBound(SheetList)
'DoEvents
Do
    DoEvents
    Application.Calculate
Loop While Not CalculationState = xlDone

Sheets(SheetList(Count)).Protect

Next
'MsgBox "Job Done"

End Sub
 
VBA Code:
Sub Optimise(Flag As Boolean)
    On Error Resume Next
    F = Not Flag
    Application.ScreenUpdating = F
    Application.DisplayAlerts = F
    Application.EnableEvents = F
    Application.DisplayStatusBar = F
    ActiveSheet.DisplayPageBreaks = F
    If F = True Then
        Application.Calculation = xlCalculationAutomatic
    Else
        Application.Calculation = xlCalculationManual
    End If
    On Error GoTo 0
End Sub

Sub Update()

    Optimise True

    SheetList = Array("Leaderboard", "Entries_by_Name", "Points_by_League", "Tables", "Admin")

    For Count = 0 To UBound(SheetList)
        ' Unprotect the sheets
        Sheets(SheetList(Count)).Unprotect
    Next Count

    Sheets("Admin").Range("A2").Value = Now

    ActiveWorkbook.RefreshAll
    DoEvents 

    With ActiveWorkbook.Worksheets("Leaderboard").Sort
        .SortFields.Clear
        .SortFields.Add Range("P1"), , xlAscending ' Sort by Sort Help column P
        .SortFields.Add Range("R1"), , xlDescending ' Sort by GD value
        .SortFields.Add Range("D1"), , xlDescending ' Sort Total column
        .SetRange Range("B1:R350")
        .Header = xlYes
        .Apply
    End With

    Sheets("Leaderboard").Select
    Range("A1").Select

    Optimise False

    For Count = 0 To UBound(SheetList)
        Sheets(SheetList(Count)).Protect
    Next Count

End Sub
 
Upvote 0
ok blocked out the lines referencing the DoEvents and reran the code still flashing up the sheet names in duplicate at bottom of excel workbook

VBA Code:
For Count = 0 To UBound(SheetList)
'DoEvents
''Do
''    DoEvents
''    Application.Calculate
''Loop While Not CalculationState = xlDone

Sheets(SheetList(Count)).Protect
Next Count
 
Upvote 0
I believe the key change that @pitchoute made that impacted the error is to take the updating of the Admin sheet out of the unprotect loop.
It is trying to update the Admin sheet before it has been unprotected.

The doubling up looks to be related to the DisplayStatusBar being turned off. If you comment out the DisplayStatusBar line, it doesn't happen.
When the code errors out it seems to try to move the Tab display line back to its original place of being the 2nd last line of the Excel application screen but doesn't have the displaystatusbar turned on to take up the last line.


Rich (BB code):
For Count = 0 To UBound(SheetList)

'Unprotect the sheets
Sheets(SheetList(Count)).Unprotect

'Add date and time to admin sheet
Sheets("Admin").Range("A2").Value = Now

Next

Sheets("Admin").Range("A2").Value = Now
 
Upvote 0
Ok tried that updated as mentioned no doubling just the blurring of tabs trying to move horizontally


1741610802139.png
 
Upvote 0
What changes did you make ?
Did you move the Admin sheet update to below the Next of the Unprotect loop ?
Did you comment out the Application.DisplayStatusBar = F ?

Now before you run the code in the immediate window paste the below and hit enter.
VBA Code:
Application.DisplayStatusBar = True
(This is to reset it to True since it might still be False after your code crashing)
 
Upvote 0
Yes did all those , updated code below that i have now
VBA Code:
Sub Optimise(Flag As Boolean)
On Error Resume Next
F = Not Flag
Application.ScreenUpdating = F
Application.DisplayAlerts = F
Application.EnableEvents = F
'Application.DisplayStatusBar = F
ActiveSheet.DisplayPageBreaks = F
If F = True Then
Application.Calculation = xlCalculationAutomatic
Else
Application.Calculation = xlCalculationManual
End If
On Error GoTo 0
End Sub

Sub Update()

'switch off updating to stop screen flickering

Optimise (True)

SheetList = Array("Leaderboard", "Entries_by_Name", "Points_by_League", "Tables", "Admin")

  For Count = 0 To UBound(SheetList)
        ' Unprotect the sheets
        Sheets(SheetList(Count)).Unprotect

    Next Count

    Sheets("Admin").Range("A2").Value = Now

' Update Macro - Download football league tables and game results from the website
'
ActiveWorkbook.RefreshAll
DoEvents


'
' Sort Macro

With ActiveWorkbook.Worksheets("Leaderboard").Sort
.SortFields.Clear
.SortFields.Add Range("P1"), , xlAscending 'Sort by Sort Help Help column P
.SortFields.Add Range("R1"), , xlDescending ' Sort by GD value
.SortFields.Add Range("D1"), , xlDescending 'Sort Total column

.SetRange Range("B1:R350")
.Header = xlYes

.Apply
End With
Sheets("Leaderboard").Select
Range("A1").Select

'allow screen to update now

Optimise (False)

'reprotect the modified sheets

For Count = 0 To UBound(SheetList)

Sheets(SheetList(Count)).Protect

Next Count

'MsgBox "Job Done"

End Sub

added your code to immediate window pressed eneter ran code and tab jumping horizontally still there
1741612441401.png
 
Upvote 0
While I could replicate your issue initially I can't replicate it using the updated code you posted.
Other than closing Excel and reopening it and/or restarting the machine, I don't have any more ideas.

PS: I don't have anything in my workbook to refresh so I don't know if that comes into play.
If you want to share your workbook via google drive or a sharing platform I am happy to take a look but I am signing off for the night now.
 
Upvote 0
I can see the issue but I don't understand why on your set up ScreenUpdating and DisplayAlerts just don't seem to be changing to False.
I don't have that issue when I create a new workbook.
I am loging off for the night will have another look tomorrow if noone else comes up with anything in the meantime.
 
Upvote 0

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