VBA在工作表名称中查找While循环

时间:2018-10-11 14:54:39

标签: excel vba

我正在尝试创建一些可在多个工作簿中使用的编码。在工作簿中,我想更新某些工作表。这些特定的工作表始终使用相同的确切格式,我希望每次都更新相同的确切单元格。

我正在尝试创建一个循环,“ Do While”编码查看工作表是否需要确定是否需要循环。

下面是我正在使用的代码,并且不断收到运行时错误“ 424”:vba中所需的对象。在剩下的代码中,我会放置一个msgbox作为占位符,以使代码正常工作。

 Do While WS.Name Like "P&L - "

If Range("S306") <> 0 Then

MsgBox ("tEST GOOD")

Worksheets(ActiveSheet.Index + 1).Select

End If

Loop

1 个答案:

答案 0 :(得分:0)

也许是这样吗?

Sub tgr()

    Dim wb As Workbook
    Dim ws As Worksheet

    'Loop through each currently open Excel workbook
    'If you instead need to loop through files in a folder, code would be different
    For Each wb In Application.Workbooks
        'loop through all sheets in one of the workbooks
        For Each ws In wb.Worksheets
            'Compare the worksheet name and cell S306 value
            If ws.Name Like "P&L - *" _
            And ws.Range("S306") <> 0 Then
                'Match found, your code would go here
                MsgBox "Workbook: " & wb.Name & Chr(10) & _
                       "Worksheet: " & ws.Name
            End If
        Next ws
    Next wb

End Sub