Vba 说明名称中包含特定字符串的工作簿是否已打开

Vba 说明名称中包含特定字符串的工作簿是否已打开,vba,excel,Vba,Excel,我有一个项目,我有一个下拉列表,里面有客户的名字。如果他们选择客户端“Anna”,并且有一个名称中包含“Anna”的工作簿,则该工作簿将被打开。如果不是,我希望一个msgbox弹出,说“那个客户端不存在” 我如何判断当前是否有打开instr(“anna”)的工作簿 这是我当前的代码,它只是查看是否只有一个工作簿打开(控制工作簿),但显然他们可以打开其他东西,所以这不是一个长期的解决方案。谢谢 strCurrPath = Application.ThisWorkbook.Path lenStrCu

我有一个项目,我有一个下拉列表,里面有客户的名字。如果他们选择客户端“Anna”,并且有一个名称中包含“Anna”的工作簿,则该工作簿将被打开。如果不是,我希望一个msgbox弹出,说“那个客户端不存在”

我如何判断当前是否有打开instr(“anna”)的工作簿

这是我当前的代码,它只是查看是否只有一个工作簿打开(控制工作簿),但显然他们可以打开其他东西,所以这不是一个长期的解决方案。谢谢

strCurrPath = Application.ThisWorkbook.Path
lenStrCurrPath = Len(strCurrPath) + 9
lenstrCurrNameSelect = Len(strCurrNameSelect)

intTotal = lenStrCurrPath + lenstrCurrNameSelect

file = Dir(strCurrPath & "\Clients\")
While (file <> "")
    If InStr(file, Sheet1.strCurrNameSelect) > 0 Then
        Workbooks.Open (strCurrPath & "\Clients\" & file)
    End If
    file = Dir
Wend

If Workbooks.Count <= 1 Then
    MsgBox ("Could not find that client workbook. Check folder.")
Else 'DoNothing
End If
strCurrPath=Application.ThisWorkbook.Path
lenStrCurrPath=Len(strCurrPath)+9
lenstrCurrNameSelect=Len(strCurrNameSelect)
intTotal=lenstrucrrpath+lenstrucrrnameselect
file=Dir(strCurrPath&“\Clients\”)
While(文件“”)
如果InStr(文件,Sheet1.strCurrNameSelect)>0,则
工作簿.打开(strCurrPath&“\Clients\”文件)
如果结束
file=Dir
温德
如果工作簿.Count简单示例:

Sub Tester()

    Dim wb As Workbook
    Set wb = ClientWorkbook("anna")
    If Not wb Is Nothing Then
        MsgBox "Found matching workbook: " & wb.Name
    Else
        MsgBox "No matching workbook"
    End If

End Sub

Function ClientWorkbook(clientName As String) As Workbook
    Dim wb As Workbook, rv As Workbook
    For Each wb In Application.Workbooks
        If UCase(wb.Name) Like "*" & UCase(clientName) & "*" Then
            Set rv = wb
            Exit For
        End If
    Next wb
    Set ClientWorkbook = rv
End Function
简单的例子:

Sub Tester()

    Dim wb As Workbook
    Set wb = ClientWorkbook("anna")
    If Not wb Is Nothing Then
        MsgBox "Found matching workbook: " & wb.Name
    Else
        MsgBox "No matching workbook"
    End If

End Sub

Function ClientWorkbook(clientName As String) As Workbook
    Dim wb As Workbook, rv As Workbook
    For Each wb In Application.Workbooks
        If UCase(wb.Name) Like "*" & UCase(clientName) & "*" Then
            Set rv = wb
            Exit For
        End If
    Next wb
    Set ClientWorkbook = rv
End Function

应用程序。工作簿
集合可以在每个项目
应用程序上使用
Instr
进行迭代和测试。工作簿
集合可以在每个项目上使用
Instr
进行迭代和测试