VBA & Macro Problems

Use a Selected Cell Value as a Worksheet Name in Excel VBA

Elise WilliamsElise Williams Nov 18, 2026 868 views

Question details

Use the active cell's value to identify and print a worksheet in a VBA macro.

Use a Selected Cell Value as a Worksheet Name in Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A macro should use a worksheet name stored in the selected cell instead of a hard-coded name.
Observed behavior
The macro should find the matching worksheet and print it, or notify the user if it does not exist.
Before you start

Make sure the active cell contains the exact name of a worksheet in the workbook that will contain the macro. Save that workbook as an Excel Macro-Enabled Workbook (.xlsm), and enable macros only if you trust the file.

Solution 1Recommended

Find and print the worksheet named in the active cell

Use a workbook-qualified worksheet reference and check whether the sheet exists before printing.

This example searches ThisWorkbook, the workbook containing the macro. If the target sheet is in a different workbook, replace ThisWorkbook with an explicit reference such as Workbooks("Report.xlsx").

1
Open the VBA editor

In Excel, open the workbook that will contain the macro and press Alt+F11 to open the Visual Basic Editor.

2
Insert the macro

Choose Insert > Module and paste this code: Sub PrintSheetNamedInActiveCell() Dim sheetName As String Dim targetSheet As Worksheet If IsError(ActiveCell.Value) Then MsgBox "The active cell contains an error." Exit Sub End If sheetName = CStr(ActiveCell.Value) If Len(sheetName) = 0 Then MsgBox "Enter a worksheet name in the active cell." Exit Sub End If On Error Resume Next Set targetSheet = ThisWorkbook.Worksheets(sheetName) On Error GoTo 0 If targetSheet Is Nothing Then MsgBox "No worksheet named '" & sheetName & "' was found." Exit Sub End If targetSheet.PrintOut End Sub

3
Select the name and run

Return to Excel, select the cell containing the worksheet name, press Alt+F8, select PrintSheetNamedInActiveCell, and click Run.

Find and print the worksheet named in the active cell
Preview before printing: The PrintOut line sends the worksheet to the printer. Replace targetSheet.PrintOut with targetSheet.PrintPreview if you want to preview it first.
Use WPS Spreadsheets

Run the worksheet-name macro in WPS Spreadsheets

In a WPS Spreadsheets installation with VBA macro support, use the macro above to find and print a sheet named in the active cell. VBA availability and compatibility can vary by edition and installation.

  1. 1. Open the workbook: Open the XLSM workbook in WPS Spreadsheets and check that your installation supports VBA macros.
  2. 2. Add the macro: Open the VBA editor from the Developer tab, insert a module, and paste the macro from the first solution.
  3. 3. Select the sheet name: Return to the spreadsheet and click the cell containing the name of the worksheet to print.
  4. 4. Run the macro: Use the Developer tab's macro controls to run PrintSheetNamedInActiveCell. If prompted, enable macros only for a trusted workbook.
Find and print a worksheet using a name stored in a cellOpen and edit Microsoft Excel formats, including XLSX and XLSMWork in a familiar spreadsheet interfaceUse a free office suite for everyday spreadsheet tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why does the macro say the worksheet was not found?

Check that the active cell contains the sheet name without unintended spaces and that the sheet is in ThisWorkbook, the workbook containing the macro. For another workbook, change the worksheet reference to that workbook.

Can I preview the sheet instead of printing it immediately?

Yes. In the VBA editor, replace targetSheet.PrintOut with targetSheet.PrintPreview, then run the macro again.

Do I need to select the target sheet before printing it?

No. Once targetSheet refers to the worksheet, targetSheet.PrintOut can print it directly without selecting it.

How do I keep the macro in my workbook?

In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). An XLSX file cannot retain VBA macros.