Workbook is Nothing

most

Board Regular
Joined
Feb 22, 2011
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. 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?

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
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
How does it not work?

If I don't have a workbook that has a name starting with 'A' I get the 'A not found!' message.
 
Upvote 0
Hi,
you have declared you object variables Public and maybe they are holding a previous value?

Rich (BB code):
Public AWorkbook As Workbook
Public BWorkbook As Workbook
Public ASheet As Worksheet
Sub GetAllNames()
Dim wb As Workbook


Set AWorkbook = Nothing
Set BWorkbook = Nothing


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!"

try adding two lines shown in RED and see if helps

Dave
 
Last edited:
Upvote 0
Yeah, it works for now also, I guess it went wrong in all testing.
Added Set AWorkbook = Nothing to minimize future issues.
Sorry for taking your time.
 
Upvote 0
Dave

Has a very good point, perhaps you shouldn't be declaring AWorkbook and BWorkbook as Public?
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top