【问题标题】:How do I apply a VBA Macro to specific worksheets that contain a certain word in their name?如何将 VBA 宏应用于名称中包含特定单词的特定工作表?
【发布时间】:2018-02-02 08:12:19
【问题描述】:

我有一个宏代码。我想将其应用于所有称为“机电需求日历”的工作表?我该怎么做?我是 VBA 新手。我假设这段代码需要进入某种循环,它将遍历所有工作表,但我对从哪里开始感到很困惑。这个宏是通过在excel中录制宏创建的(因为我还不擅长写VBA)

Sub v49()
'
' Macro2 Macro
'

     Worksheets("M&E Demand Calendar (S.S)").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

【问题讨论】:

  • 这完全取决于你的“宏代码”是什么,而你没有发布它。
  • @braX 我已经添加了代码,任何帮助将不胜感激
  • 我建议您更改宏以接受工作表作为参数。另外 摆脱所有的 select 语句。见VBA Best Practices

标签: vba excel


【解决方案1】:

这样的事情可以解决问题。如果没有,请提供更多信息:

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

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

【讨论】:

    【解决方案2】:

    我建议使用一个循环来检查每个工作表的名称,然后如果它是您想要的名称,那么您可以调用此宏。即类似

    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
    

    您必须对当前宏进行的唯一真正更改是删除第一行。尽管我建议研究一些编码最佳实践。 Ja72 很乐意将它们链接到您帖子的 cmets 中。但这应该可以帮助您完成大多数事情

    这不区分大小写,因此它会看到“THIS”和“this”以及“ThIs”。如果出于某种原因这是相关的,那么您可以将其“vbTextCompare”更改为“vbBinaryCompare”,尽管我怀疑这是否有必要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      相关资源
      最近更新 更多