VBA & Macro Problems

Fix a VBA Type Mismatch When Selecting an Excel Worksheet and Cell

Ayan MasoodAyan Masood Dec 2, 2026 869 views

Question details

The user needs to select an Excel worksheet and cell using values stored in VBA variables.

Fix a VBA Type Mismatch When Selecting an Excel Worksheet and Cell
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro constructs a worksheet name and cell reference from variables that may contain values from cells E3, E4, and E5.
Observed behavior
The macro produces an error when selecting the worksheet and cell. Quoting the variable name refers to a literal worksheet name instead of the variable's value.
Before you start

Open the workbook in Excel, press Alt+F11 to inspect the macro, and check the values and types stored in the worksheet-name and cell-reference variables. Confirm that the target worksheet exists in the workbook.

Solution 1Recommended

Use the worksheet variable and build a valid Range address

Use this approach when the cell address is formed by joining a column letter with row digits.

1
Inspect the variable values

In the VBA editor, check that Tmth contains the target worksheet name and that joining Tday and Tstore after "A" produces a valid row number. For example, Tday = 123 and Tstore = 7 produce A1237.

2
Replace the worksheet reference

In the macro, replace Sheets("Tmth") with a reference to the variable, such as ThisWorkbook.Worksheets(CStr(Tmth)). Quotation marks around Tmth would instead refer to a sheet literally named Tmth.

3
Activate and select the cell

In the macro, assign the sheet with `Set ws = ThisWorkbook.Worksheets(CStr(Tmth))`, then use `ws.Activate` followed by `ws.Range("A" & Tday & Tstore).Select`. Declare ws as a Worksheet, and run the macro to check the result.

Use the worksheet variable and build a valid Range address
Check the workbook and address: ThisWorkbook refers to the workbook containing the macro; use a different workbook reference if the target sheet is elsewhere. A missing sheet, invalid address, or incorrect variable type can cause a different VBA error.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for an easier move between applications. It does not fix errors in Excel VBA macros.

Open and edit spreadsheet data in a familiar interfaceCompatible with Microsoft Excel formats, including XLSXReview worksheet names and cell values used by a macroFree, lightweight software for everyday office work
QA img-9

Frequently Asked Questions

Why does Sheets("Tmth") not use my variable?

Quotation marks make "Tmth" a literal sheet name. Use Worksheets(CStr(Tmth)) to look up the sheet whose name is stored in the variable.

When should I use Range instead of Cells?

Use Range when you have a valid address such as "A1237". Use Cells(row, column) when the values are separate numeric coordinates, such as Cells(3, 4) for D3.

Why can Select fail even if the cell reference is valid?

The target worksheet must be active for its cell to be selected. Activate it with ws.Activate before calling ws.Range(...).Select, or work with the Range directly if selection is unnecessary.

How can I check what is causing the VBA error?

Open the VBA editor with Alt+F11, inspect the variables in the macro, and verify the worksheet name and resulting cell address. A type mismatch, missing worksheet, and invalid address may produce different errors.