'VBA Freeze panes causes graphical issues
I really hope someone can share some knowledge about this, as google has not been kind to me. I run a macro, which turns OFF at the beginning and ON at the end in reverse order:
Application.DisplayAlerts
Application.ScreenUpdating
Application.EnableEvents
If Workbooks.Count > 0 Then
If Application.Calculation
End if
The problem is after I run this macro, and I go to a sheet that has freeze panes, I get a weird graphical glitch on the top part of the frozen pane, that looks like a preview of another sheet.
If I manually save the file, the glitch goes away. Can anyone tell me of a way to force refresh the sheet without saving the file?
If I save the file after the macro is done (as the last thing I do) the issue does not go away.
If I force recreate the freeze panes using the macro below, the problem still persists:
Sub FreezePanes_BySheetRef(Optional howManyRows, Optional howManyCols, Optional targetSh As Worksheet, Optional applyFilter As Boolean, Optional autoFit As Boolean, Optional Zoom, Optional turnOffGridlines As Boolean)
'ALLOWS FORMATTING WITHOUTH THE SHEET BEING SELECTED
'Handle missing
If targetSh Is Nothing Then: Set targetSh = ActiveSheet
If IsMissing(howManyRows) Then: howManyRows = 1
If IsMissing(howManyCols) Then: howManyCols = 1
If IsMissing(applyFilter) Then: applyFilter = False
'If IsMissing(autoFit) Then: autoFit = False
Dim previousSH
Set previousSH = ActiveSheet 'RECORD PREVIOUS SHEET TO GET BACK TO IT
Dim View As WorksheetView
'On Error Resume Next
With targetSh
.Activate
If Not IsMissing(Zoom) Then
ActiveWindow.Zoom = Zoom
End If
For Each View In .Parent.Windows(1).SheetViews
If View.Sheet.name = .name Then
'ONLY IF SHEET WAS FOUND
With View
.DisplayGridlines = Not turnOffGridlines 'BY DEFAULT = FALSE
With ActiveWindow
If .FreezePanes Then
.FreezePanes = False
.SplitColumn = 0
.SplitRow = 0
End If
.SplitColumn = howManyCols
.SplitRow = howManyRows
.FreezePanes = True
End With
End With
Exit For
End If
Next
If applyFilter Then
On Error Resume Next
.AutoFilterMode = False
.range(.cells(howManyRows, 1), .cells(howManyRows, getLastCol(1, 19, .name))).AutoFilter
On Error GoTo 0
End If
If autoFit Then
.cells.Columns.autoFit
End If
End With
previousSH.Activate 'GET BACK TO PREVIOUS SHEET
End Sub
Solution 1:[1]
I have found sort of a fix. If you Hide/Unhide the sheet the graphical glitch goes away...
Solution 2:[2]
Zooming in/out also fixes it. However, I'm still looking for a way to prevent it in the first place.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dumitru Daniel |
| Solution 2 | Dave Sanders |
