most
Board Regular
- Joined
- Feb 22, 2011
- Messages
- 107
- Office Version
- 365
- 2019
- Platform
- Windows
- Mobile
My code looks through all workbooks to find the complete name of the workbook.
I want to add a check if the workbook is not found.
I thougt this would work, but it doesn't.
If AWorkbook Is Nothing Then MsgBox "A not found!"
Any pointers?
I want to add a check if the workbook is not found.
I thougt this would work, but it doesn't.
If AWorkbook Is Nothing Then MsgBox "A not found!"
Any pointers?
Code:
Public AWorkbook As Workbook
Public BWorkbook As Workbook
Public ASheet As Worksheet
Sub GetAllNames()
Dim wb As Workbook
For Each wb In Application.Workbooks
If Left(Trim(wb.Name), 1) = "A" Then
Set AWorkbook = Workbooks(wb.Name)
End If
If Left(Trim(wb.Name), 1) = "B" Then
Set BWorkbook = Workbooks(wb.Name)
End If
Next wb
If AWorkbook Is Nothing Then MsgBox "A not found!"
If BWorkbook Is Nothing Then MsgBox "B not found!"
Dim sh As Worksheet
For Each sh In BWorkbook.Worksheets
If Left(Trim(sh.Name), 1) = "A" Then
Set ASheet = Worksheets(sh.Name)
Count = Count + 1
End If
Next sh
If ASheet Is Nothing Then MsgBox "A sheet not found!"
If Count > 1 Then MsgBox "Several A sheets found!"
End Sub