vba 如何在工作表名称中搜索字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6860212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:43:03  来源:igfitidea点击:

How to search a string in a sheetname?

excelvbaexcel-vba

提问by Suman

I am trying to search a particular string in all sheet names of a workbook using For each loop as follows:

我正在尝试使用 For each 循环在工作簿的所有工作表名称中搜索特定字符串,如下所示:

 For Each Sheet In ActiveWorkbook.Sheets
  Name = ActiveSheet.Name
  Next Sheet

But i am not able to get all the sheet names using above code as it gets stuck up only one sheet name.

但是我无法使用上面的代码获取所有工作表名称,因为它只会卡住一个工作表名称。

After getting the sheet name i want to search a particular stringin the sheet name So it would be great if any one can help me in searching a string in all sheets.

获得工作表名称后,我想在工作表名称中搜索特定字符串因此,如果有人可以帮助我在所有工作表中搜索字符串,那就太好了。

回答by Jacob

For Each sheet In ActiveWorkbook.Sheets
    If sheet.Name Like "*" & strSearch & "*" Then
        Debug.Print "Found! " & sheet.Name
    End If
Next

You iterate over all sheets, but you always use ActiveSheet.Namein your loop. To search for a pattern, you can use Likewith the *wildcard.

您遍历所有工作表,但始终ActiveSheet.Name在循环中使用。要搜索的模式,你可以使用Like*通配符。

回答by Tiago Cardoso

You can use the InStrfunction as well... or maybe I didn't get your question correctly.

您也可以使用InStr函数……或者我可能没有正确回答您的问题。

Sub CheckSheets()        
    Dim oSheet As Excel.Worksheet
            For Each oSheet In ActiveWorkbook.Sheets           
        If InStr(UCase(oSheet.Name), UCase("Sheet")) Then                
            Debug.Print oSheet.Name                
        End If            
    Next oSheet        
End Sub