VBA List Open Unsaved Workbooks in Excel

VBA List Open Unsaved Workbooks in Excel. It helps to know all open and unsaved workbooks name. It is easy shift between unsaved workbooks. We have an option to open multiple workbooks in MS Office Excel. Let us see in the following tutorial how to list all open unsaved Workbook names in Excel VBA.

Syntax to VBA Loop through all Workbooks in Excel

Here is the following syntax to loop through all Workbooks in Excel VBA.

For Each Workbook in Application.Workbooks
      Statements...
Next

Where Workbook: It represents Workbook object which is part of workbooks collection.

Macro to List Open Unsaved Workbooks in Excel VBA

Let us see the following example macro to List all Open unsaved Workbooks in Excel VBA.

'VBA List All Open Unsaved Workbooks in Excel
Sub VBA_List_All_Open_Unsaved_Workbooks()
    
    'Variable declaration
    Dim xWorkbook As Workbook
        
    'Loop through all workbooks
    For Each xWorkbook In Application.Workbooks
        
        'Check Workbook is current Workbook or not
        If xWorkbook.Name <> ThisWorkbook.Name Then
            'Check if the file names has an extension
            
            If InStr(Right(xWorkbook.Name, 5), ".xl") > 0 Then
                'Save and Close Workbook
                xWorkbook.Close SaveChanges:=True
            Else
                Debug.Print xWorkbook.Name
            End If
        End If
    Next

End Sub

Instructions to use Macro

Here are the instructions to use above macro procedure in Visual basic editor.

  • Save Visual Basic Editor(VBE) by clicking Alt +F11
  • Go to code window by clicking F7
  • Copy above specified macro or procedure
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • You can see output on the Immediate Window (Ctrl+G) screen

Related Workbook Object articles

You may also like the related Workbook Object articles.

VBA Workbook Object




Leave a Comment

Your email address will not be published. Required fields are marked *