使用Like运算符浏览不同的工作簿

时间:2019-03-07 16:33:04

标签: excel vba

我有两个不同的工作簿将被生成并保持打开状态。名称包含“出站”或“入站”,然后是日期和时间。例如“ data-details-outbound 2019-03-07 ...”和“ data-details-inbound 2019-03-07 ...”。我需要在每个工作表上执行不同的代码,因此我需要我的代码来区分两者。

我有以下代码,但是当我运行它时,它只对激活的工作簿执行所有操作。

Sub NotifiedReminderEmail()
'
' Current issue is the LIKE operator is choosing whatever wb is open, not the correct one.
'
    Dim wb As Workbook
    Dim owbName As String
    Dim iwbName As String

    owbName = "outbound"
    iwbName = "inbound"

    'sets up the OB sheet
    For Each wb In Application.Workbooks
        If wb.Name Like "*" & owbName & "*" Then
            Debug.Print wb.Name
            With wb.Worksheets(1)
                'perform code
            End With
        End If
    Next wb

    'sets up the IB sheet
    For Each wb In Application.Workbooks
        If wb.Name Like "*" & iwbName & "*" Then
            Debug.Print wb.Name
            With wb.Worksheets(1)
                'perform code
            End With
        End If
    Next wb

End Sub

0 个答案:

没有答案