Use a Selected Cell Value as a Worksheet Name in Excel VBA
Question details
Use the active cell's value to identify and print a worksheet in a VBA macro.

- 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.
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.
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").
In Excel, open the workbook that will contain the macro and press Alt+F11 to open the Visual Basic Editor.
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
Return to Excel, select the cell containing the worksheet name, press Alt+F8, select PrintSheetNamedInActiveCell, and click Run.

Select a worksheet using the active cell value
Use a short statement when you only need to select a sheet and know that its name is valid.
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. Open the workbook: Open the XLSM workbook in WPS Spreadsheets and check that your installation supports VBA macros.
- 2. Add the macro: Open the VBA editor from the Developer tab, insert a module, and paste the macro from the first solution.
- 3. Select the sheet name: Return to the spreadsheet and click the cell containing the name of the worksheet to print.
- 4. Run the macro: Use the Developer tab's macro controls to run PrintSheetNamedInActiveCell. If prompted, enable macros only for a trusted workbook.

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.




