【问题标题】:VBA Do While Loop Looking within Worksheet NameVBA Do While 循环在工作表名称中查找
【发布时间】:2018-10-11 23:01:48
【问题描述】:

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

我正在尝试创建一个循环,并且“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

【问题讨论】:

标签: excel vba


【解决方案1】:

也许是这样的?

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

【讨论】:

  • 谢谢,这就是我想要完成的。我一直在尝试使用 do while 函数。
猜你喜欢
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2016-08-24
  • 2018-01-16
  • 1970-01-01
相关资源
最近更新 更多