Description:
When we are working with workbooks and worksheets, some times we may required to Get Active Workbook or Worksheet Name, Path of the workbook to know the directory, FullName(Complete path) of the workbook to know the location of the workbook, selected Range address in active sheet or selected Cell address in active sheet using Excel VBA.
Solution(s):
You can get Active Workbook Or Worksheet Name by using Name property of the workbook or worksheet.
Get Active Workbook or Worksheet Name – Example Cases:
- Get an Active Workbook Name
- Get an Active Workbook Path
- Get an Active Workbook FullName
- Get an Active Worksheet Name
- Get an Active Range Address
- Get an Active Cell Address
Get an Active Workbook Name
You can use ActiveWorkbook property to return the active workbook name. You can use the following code to get the name of the Active Workbook
Code:
Sub DisplayWorkbookName() MsgBox ActiveWorkbook.Name, vbInformation, "Workbook Name" End Sub
Output:
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- Press F5 to see the output
- You should see output as shown above
- Save the file as macro enabled workbook
Get an Active Workbook Path
You can use ActiveWorkbook property to return the active workbook Path.You can use the following code to Get Active Workbook Path to know the workbook directory.
Code:
Sub DisplayWorkbookPath() MsgBox ActiveWorkbook.Path, vbInformation, "Workbook Path" End Sub
Output:
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- Press F5 to see the output
- You should see output as shown above
- Save the file as macro enabled workbook
Get an Active Workbook FullName
You can use ActiveWorkbook property to return the active workbook FullName. You can use the following code to get Active Workbook FullName to know the location of workbook.
Code:
Sub DisplayWorkbookFullName() MsgBox ActiveWorkbook.FullName, vbInformation, "Workbook Complete Path" End Sub
Output:
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- Press F5 to see the output
- You should see output as shown above
- Save the file as macro enabled workbook
Get an Active Worksheet Name
You can use ActiveSheet property to return the ActiveSheet Name. You can use the following code to get Active Worksheet Name.
Code:
Sub DisplayWorkSheetName() MsgBox ActiveSheet.Name, vbInformation, "Active Sheet Name" End Sub
Output:
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- Press F5 to see the output
- You should see output as shown above
- Save the file as macro enabled workbook
Get an Active(Selected) Range Address
You can use Address property of the selected range(Selection Method). You can use the following code to get the selected range address in active sheet.
Code:
Sub SelectedRangeAddress() 'Variable Declaration Dim MyRange As String 'Assign Selected Range Address to variable MyRange = Selection.Address 'Display Output MsgBox MyRange, vbInformation, "Range Address" '-----------------------(OR)------------------ MsgBox Selection.Address, vbInformation, "Range Address" End Sub
Output:
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- Select a range from B2 to E11 in active sheet
- Goto code window and Press F5 to see the output
- You should see output as shown above
- Save the file as macro enabled workbook
Get an Active(Selected) Cell Address
You can use Address property of the selected cell(Selection Method). By using the following code you can get the selected cell address in active sheet.
Code:
Sub SelectedCellAddress() 'Variable Declaration Dim MyCell As String 'Assign Selected Range Address to variable MyCell = Selection.Address 'Display Output MsgBox MyCell, vbInformation, "Cell Address" '-----------------------(OR)------------------ MsgBox Selection.Address, vbInformation, "Cell Address" End Sub
Output:
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- Select a cell F5 in active sheet
- Goto code window and Press F5 to see the output
- You should see output as shown above
- Save the file as macro enabled workbook
thank you this is helpful