(VBA)适用于所有,除了一个特定的

嗨,大家好,我使用VBA模块在Excel中做了一个button,代码在活动工作表上工作,但是我要查找的是应用于更多工作表,而不仅仅是放置button的活动工作表。

Sub Botón1_Haga_clic_en() Call Worksheet_Calculate End Sub 'apply cells colors from single-cell formula dependencies/links Private Sub Worksheet_Calculate() Dim Cel As Range Dim RefCel As Range On Error Resume Next For Each Cel In ActiveSheet.UsedRange If Cel.HasFormula Then Set RefCel = Evaluate(Mid(Cel.Formula, 2)) Cel.Interior.Color = RefCel.Interior.Color End If Next Cel End Sub 

试试下面的代码:

 Option Explicit Sub Botón1_Haga_clic_en() Dim wsName As String Dim ws As Worksheet wsName = ActiveSheet.Name For Each ws In ThisWorkbook.Worksheets If Not ws.Name Like wsName Then '<-- is worksheet's name doesn't equal the ActiveSheet's ApplyCellColors ws ' <-- call you Sub, with the worksheet object End If Next ws End Sub '======================================================================= 'apply cells colors from single-cell formula dependencies/links Private Sub ApplyCellColors(ws As Worksheet) Dim Cel As Range Dim RefCel As Range On Error Resume Next For Each Cel In ws.UsedRange If Cel.HasFormula Then Set RefCel = Evaluate(Mid(Cel.Formula, 2)) Cel.Interior.Color = RefCel.Interior.Color End If Next Cel End Sub 

你的问题可以被翻译成类似于如何遍历所有表并忽略其中的一个?

这是一个很好的方法来做到这一点:

 Option Explicit Option Private Module Public Sub TestMe() Dim wks As Worksheet For Each wks In ThisWorkbook.Worksheets If wks.name = "main" Then Debug.Print "Do nothing here, this is the active sheet's name" Else Debug.Print wks.name End If Next wks End Sub 

很确定,你应该能够适应你的代码。