如何将VBAmacros应用于包含名称中特定单词的特定工作表?

我有一个macros代码。 我想将其应用于所有名为“M&E需求日历”的工作表? 我将如何做到这一点? 我是VBA新手。 我认为这个代码需要进入某种循环,这将通过所有的工作表,但我很困惑从哪里开始。 这个macros是由Excel中的录制macros创build的(因为我还不擅长编写VBA)

Sub v49() ' ' Macro2 Macro ' Worksheets("M&E Demand Calendar (SS)").Select Cells.Select Cells.FormatConditions.Delete Cells.Select Selection.FormatConditions.Add Type:=xlTextString, String:="High", _ TextOperator:=xlContains Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 255 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False Cells.FormatConditions.Delete Cells.Select Selection.FormatConditions.Add Type:=xlTextString, String:="High", _ TextOperator:=xlContains Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 255 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False Cells.Select Selection.FormatConditions.Add Type:=xlTextString, String:="Medium", _ TextOperator:=xlContains Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 49407 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False Cells.Select Selection.FormatConditions.Add Type:=xlTextString, String:="Low", _ TextOperator:=xlContains Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 15773696 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False Cells.Select Selection.FormatConditions.Add Type:=xlTextString, String:="Distress", _ TextOperator:=xlContains Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False Sheets("Hotel Settings").Select Range("I6").Select ActiveCell.FormulaR1C1 = "49" End Sub 

像这样的事情可以做到这一点。 如果没有,给更多的信息:

 Public Sub TestMe() Dim ws As Worksheet Dim strName As String strName = "M&E Demand Calendar" For Each ws In ThisWorkbook.Worksheets If InStr(1, ws.name, strName) > 1 Then MyMacro ws.name Next ws End Sub Private Sub MyMacro(str As String) Debug.Print str End Sub 

它检查工作表的名称是否包含带InStrstrName

我会build议一个循环检查每个工作表名称,然后如果它是你想要的名称,那么你可以调用这个macros。 即类似的东西

 sub v49SheetCheck() dim counter1 as long for counter1 = 1 to thisworkbook.sheets.count if 0 < instr(1,Sheets(counter1).name,"Thing to look for",vbTextCompare)_ then 'this is here because all your code requires is that the target sheet be active sheets(counter1).activate call v49 end if next counter1 end sub 

唯一真正的改变,就是你必须对当前的macros做出改变,就是摆脱第一行。 虽然有一些编码的最佳实践,我会build议研究。 Ja72非常友好,可以将它们链接到您的post的评论中。 但这应该让你通过大多数的事情

这是不区分大小写的,所以它会看到“THIS”,“this”和“THIs”一样。 如果这是相关的某种原因,那么你可以改变它“vbTextCompare”为“vbBinaryCompare”,虽然我怀疑这将是必要的。