vba 计算名称以特定文本开头的工作表的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15138915/
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 19:49:29  来源:igfitidea点击:

Counting the number of Worksheets with name that starts with specific text

excelvba

提问by user2120103

I am trying to count the number of worksheets with similar names (e.g. all that start with "East").

我正在尝试计算名称相似的工作表的数量(例如所有以“East”开头的)。

Here's the code I'm using to count the sheets:

这是我用来计算床单的代码:

Dim wb As Workbook
Dim ws As Worksheet
Dim myTotal As Long
Dim wsTotal As Long

For Each wb In Workbooks
    For Each ws In Worksheets
        If ws.Name Like "20 Out of Court" & "*" Then myTotal = myTotal + 1
    Next ws
Next wb
wsTotal = myTotal

Sometimes it doubles the worksheet total on certain machines.

有时它会将某些机器上的工作表总数加倍。

回答by user2120103

Try Below codee :

尝试以下代码:

Sub sample()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim myTotal As Long
    Dim wsTotal As Long

    For Each wb In Workbooks
        For Each ws In Worksheets
            Debug.Print ws.Name
            If InStr(1,left(ws.Name,4), "East", vbTextCompare) > 0 Then ' will check for start with "East"..
                myTotal = myTotal + 1
            End If
        Next ws
    Next wb
    wsTotal = myTotal
End Sub