2017-08-24 13 views
1

私はマクロコードを持っています。 "M & E Demand Calendar"のように呼ばれるすべてのワークシートに適用したいですか?どうすればいい?私はVBAの初心者です。私は、このコードは、すべてのワークシートを通過するいくつかの種類のループに入る必要があると思いますが、どこで開始するのか混乱します。このマクロは、(私はまだVBAを書くのが得意じゃないと)名前に特定の単語を含む特定のワークシートに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 
+0

それはあなたの "マクロコード"が何であるかに依存し、あなたはそれを投稿しませんでした。 – braX

+0

@braXコードを追加しましたが、助けとなるでしょう – Sorath

+0

マクロを変更して、ワークシートを引数として受け入れることをお勧めします。また、_getはすべてのselect文を削除します。 [VBAベストプラクティス](https://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices#t=201708241358024567181) – ja72

答えて

2

このような何かが、トリックを行うことができますExcelでマクロを記録することにより作成されました。ない場合は、より多くの情報を与える:

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含まれているかどうかをチェックします。

+0

最新の質問をご覧ください – Sorath

0

私は各シート名をチェックするループを提案し、それがあなたが望む名前であれば、このマクロを呼び出すことができます。

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はあなたの投稿のコメントにリンクするのに十分親切でした。しかし、これはほとんどのものを通してあなたを得るはずです

これは大文字と小文字を区別しないので "THIS"と同様に "これ"と "これ"が表示されます。それが何らかの理由で関連している場合は、それが必要であるかどうかは疑問ですが、 "vbTextCompare"から "vbBinaryCompare"に変更できます。

関連する問題