79578936

Date: 2025-04-17 09:47:31
Score: 1
Natty:
Report link

With the input from all the comments, this is what currently does the job:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Sub:      RemoveExpressionsFromWorkbook()
' Purpose:  Replace all formula by their results, essentially freezing the Workbook in its current state
' Source:   n.a.
' Arguments: none
'
' CAVEAT:   This code will alter the contents of this workbook permanently.
'           Before running this code, you should store this workbook with a different name to avoid
'           accidentally overwriting the original file.
'
' Authors:  Friedrich
'
' Comments:
'----------------------------

Sub RemoveExpressionsFromWorkbook()
    Dim ws As Worksheet
    
    ' Disable background tasks
    Call TurnEverythingOff

    For Each ws In ActiveWorkbook.Worksheets
        Debug.Print ws.Name; ": "; ws.UsedRange.Address
        
        ' original code: Does not work correctly because is also removes named tables
        ' the reason for that is not known --> TODO!
'       ws.UsedRange.Value = ws.UsedRange.Value

        ws.UsedRange.Copy
        ws.UsedRange.PasteSpecial xlPasteValues
        
        ' this should de-select the used range, but it does not work --
        ' the selection remains on all worksheets
'       Application.CutCopyMode = False
        
        ' so instead, we actively select the home position cell.
        ' not clear if this will work if events are disabled?
        ' Maybe it's just delayed, and will become active after events are reenabled.
        ws.Activate
        ws.Range("A1").Select

    Next

    ' Restore prior event settings
    Call RestoreEverything
    
End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Sub:      TurnEverythingOff(), RestoreEverything()
' Arguments: none
' Purpose:  Switch off all automatic background processes, and restore them
'
' Source:   https://stackoverflow.com/questions/43801793/turn-off-everything-while-vba-macro-running
' Authors:  Subodh Tiwari sktneer, "YowE3K"
'
' Modifications:
' 2025/03/06 Friedrich: combine all on/all off code with restore-to-prior-value
'
' Comments:
'----------------------------

Sub TurnEverythingOff()
    With Application
        ' store old values in globals
        ' no recursion allowed, so don't call multiple times w/o restoring!
        origCalculation = .Calculation
        origEnableEvents = .EnableEvents
        origDisplayAlerts = .DisplayAlerts
        origScreenUpdating = .ScreenUpdating

        ' switch everything off
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
End Sub


Sub RestoreEverything()
    With Application
        .Calculation = origCalculation
        .EnableEvents = origEnableEvents
        .DisplayAlerts = origDisplayAlerts
        .ScreenUpdating = origScreenUpdating
    End With
End Sub
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Friedrich