确定是否打开名称中具有特定string的工作簿

我有一个项目,我有一个下拉满了客户的名字。 如果他们select客户端“安娜”,并且有名字中有“安娜”的工作簿,则打开该工作簿。 如果不是,我想要一个msgboxpopup“说客户端不存在”

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

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

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 

简单的例子:

 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