VBA – Test if Workbook is Open by Workbook Name

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on August 12, 2019

Test if Workbook s Open

Here is some real basic code for testing if a workbook is currently open by testing for a workbook’s name, not the fully qualified name including the path.

I was writing code in a spreadsheet that altered sheets in another workbook, and wanted to make sure workbook 2 was open before the code executed in workbook 1.

Sub TestByWorkbookName()
Dim wb As Workbook

    For Each wb In Workbooks
        If wb.Name = "New Microsoft Excel Worksheet.xls" Then
            MsgBox "Found it"
            Exit Sub 'call code here, we'll just exit for now
        End If

End Sub

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text


Learn More!

<<Return to VBA Examples


VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples