To get the name of the currently active workbook in Excel VBA, use the ActiveWorkbook.Name property. This returns the file name including extension (e.g., "Budget.xlsx") of the workbook that has focus. No additional setup is needed if you're working within the VBA editor of that workbook.

What You'll Need

  • Microsoft Excel (any recent version that supports VBA)
  • A workbook open in Excel
  • Access to the VBA editor (press ALT + F11)

Step-by-Step

  1. Open the workbook whose name you want to retrieve.
  2. Press ALT + F11 to launch the VBA editor.
  3. In the Project Explorer window, locate your workbook under "VBAProject (YourWorkbookName)".
  4. Right-click the workbook name, choose Insert, then Module to create a new code module.
  5. In the code window, type or paste the following subroutine:
  6. Sub GetActiveWorkbookName()
        MsgBox "The name of the active workbook is: " & ActiveWorkbook.Name
    End Sub
  7. Place your cursor anywhere inside the subroutine and press F5 to run it, or close the editor and run it via ALT + F8 in Excel.
  8. A message box will appear displaying the exact name of the active workbook, including its file extension.
  9. To use this value in other code, assign it to a variable: Dim wbName As String: wbName = ActiveWorkbook.Name.

Common Mistakes

  • Using ThisWorkbook.Name when you need the active workbook: ThisWorkbook always refers to the workbook containing the code, not necessarily the one the user is viewing.
  • Assuming the name excludes the file extension: ActiveWorkbook.Name includes the extension (e.g., .xlsx, .xlsm); use Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") - 1) to strip it if needed.
  • Running the code when no workbook is active: ensure a workbook is open and focused before executing.
  • Confusing the workbook name with the worksheet name: worksheets are accessed via ActiveSheet.Name, which is different.
  • Forgetting to enable macros: if security settings block VBA, the code won't run; adjust macro settings in File > Options > Trust Center.

Tips

  • Use ActiveWorkbook.FullName if you need the full file path along with the name.
  • To avoid errors when no workbook is active, wrap the code in error handling: On Error Resume Next followed by a check for ActiveWorkbook Is Nothing.
  • Store the workbook name in a public variable if multiple procedures need it, reducing repeated calls to ActiveWorkbook.
  • When looping through workbooks, use Workbooks(i).Name instead of relying on ActiveWorkbook, which can change.
  • Use Debug.Print ActiveWorkbook.Name to send the name to the Immediate Window (CTRL + G in VBA editor) for quick testing without pop-ups.

Troubleshooting

  • Nothing happens when I run the code: Check that macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select "Enable all macros" (temporarily for testing) or "Disable all macros with notification" and click "Enable Content" when prompted.
  • Wrong name appears: You may have activated another workbook (e.g., Personal.xlsb or an add-in). Click the correct workbook window before running the code, or specify the workbook explicitly: Workbooks("YourFile.xlsx").Name.
  • Compile error: Expected: This usually means a typo in the code. Verify that ActiveWorkbook is spelled correctly and that the subroutine lines are complete.
  • Name appears without extension: You might be looking at ThisWorkbook in a macro-enabled workbook where the code assumes a different format. Double-check which object you're using.
  • Error 9: Subscript out of range: Occurs if you try to access a workbook by name that isn't open. Ensure the workbook is open and the name is spelled exactly as shown in the title bar.

Frequently Asked Questions

Can I get the name of a workbook that is not active?

Yes. Use Workbooks("Filename.xlsx").Name to get the name of any open workbook by its exact filename, or loop through Workbooks collection to check each one.

Does the workbook name include the file path?

No. ActiveWorkbook.Name returns only the filename and extension. For the full path, use ActiveWorkbook.FullName.

What if the workbook hasn't been saved yet?

An unsaved workbook will have a temporary name like "Book1", "Book2", etc., until you save it and assign a proper filename.

Can I change the workbook name using VBA?

Not directly. The workbook name is tied to the saved filename. To change it, you must save the workbook with a new name using ActiveWorkbook.SaveAs "NewName.xlsx".

Is there a difference between ThisWorkbook and ActiveWorkbook?

Yes. ThisWorkbook always refers to the workbook where the VBA code is stored. ActiveWorkbook refers to the workbook currently in focus, which may be different if the user switched to another workbook.

Why does my macro return the wrong workbook name after opening a new file?

Because ActiveWorkbook updates to the most recently activated workbook. If your code opens a new workbook and doesn't redirect focus, it will return the new one's name. Explicitly activate the desired workbook before checking its name.