遍历具有特定名称的工作表 [英] Loop through worksheets with a specific name
问题描述
说我有10个工作表,而4个具有非常相似的名称:
Say I have 10 worksheets and 4 have very similar names:
1.危险的汤姆
2.危险男人
3.危险十 4.danger lan
1.danger tom
2.danger man
3.danger ten
4.danger lan
我希望我的代码查找名称中带有文本字符串 danger 的所有工作表并执行我的代码
I want my code to find all worksheets with the text string danger in its name and execute my code
我尝试了以下两个实例:
I've tried the following two instances:
Public Sub SubName()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "danger" Then
*Also tried this version: If ws.Name Like "danger" = True Then*
Range("A1").Interior.ColorIndex = 37
End If
Next ws
End Sub
服用2
Sub WorksheetLoop()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If InStr("danger", ws.Name) > 0 Then
Range("A1").Interior.ColorIndex = 37
End If
Next ws
End Sub
即使没有错误,第一个代码也不会执行任何操作.第二个代码仅在名称与我写下的内容完全匹配时才起作用.
The first code doesn't do anything, even though there's no errors. The second code only does something if the name matches exactly with what I wrote down.
需要任何帮助.
推荐答案
如果您对此进行更改,我认为您的第一个代码将起作用:
I think Your first code will work if you change this:
If ws.Name Like "danger" Then
*Also tried this version: If ws.Name Like "danger" = True Then*
Range("A1").Interior.ColorIndex = 37
End If
对此:
If ws.Name Like "danger" & "*" Then
ws.Range("A1").Interior.ColorIndex = 37
End If
您只需使用危险*"即可.
and simply you can use "danger*".
这篇关于遍历具有特定名称的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!