0

I have a macro that is currently running within 4 worksheets that highlights cell rows within a certain range (the ranges differ for each worksheet) but to keep the worksheet looking clean and not to leave the highlighting it has built in to it a line that tells it to clear highlighting when I click cell A6 in each of the worksheets where the macro is contained. My issue is getting others who use the worksheet to follow this method, so I am trying to see if there is a way to use the Workbook_BeforeSave workbook function to clear all highlighting on the worksheets when the file is saved.

Is there a way to iterate the "clear formatting" sub that exists in each worksheet from the Workbook module? The clearing code in the worksheet modules is as follows (but I cannot seem to get it to function within the workbook module):

Dim bInRange As Boolean
Static rOld As Range

If Not bInRange Then
Set rOld = Nothing
Exit Sub
End If
4

2 回答 2

2

为您的代码创建一个子。像这样的东西。

Private sub RunMyCode()
    Dim bInRange As Boolean
    Static rOld As Range

    If Not bInRange Then
        Set rOld = Nothing
        Exit Sub
    End If
End sub

Private Sub Workbook_BeforeClose(Cancel as Boolean)
    'Call it before your workbook is closed
    RunMyCode
End Sub
于 2015-07-17T15:03:13.117 回答
1

您可以像这样运行代码

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

call sheet1.clear_formatting
call sheet2.clear_formatting
call sheet3.clear_formatting
call sheet4.clear_formatting
end sub

sheet1 是该工作表的代码名称,如果更容易,您也可以使用 sheet("sheet1").clear_formatting

或者如果你有很多床单,你可以做

for each ws in activeworkbook.sheets
    call ws.clear_formatting
next
于 2015-07-17T15:03:19.480 回答