我有一个项目,我的下拉列表中包含客户名称。如果他们选择客户端“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
答案 0 :(得分:2)
简单示例:
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