Problem Description
Today, I met a child shoes in the group. I said that a piece of code always reports errors and has unknown causes. The code is similar to the following:
| The code is as follows: |
Copy code |
Sub ErrDemo () On Error GoTo Emsg Dim I As Long Dim SName As String For I = 1 To 4 Sheets ("Sheet1"). Cells (I, 1). Value). Select Next Exit Sub Emsg: MsgBox "Error! " End Sub |
The main purpose is to scan the Sheet names saved in cells to enable different Sheet activation and display.
Cause
Let's take a look at his code and look at the name of the Sheet stored in the cell. We can draw a conclusion that the data type is ambiguous, leading to errors, because some Sheet names of this kid's shoes are numbers directly, and those who are familiar with Vba should know that Sheets ("Sheet name") or Sheets (SheetID) can all locate Sheet, then you pass a number to the past and Sheets will recognize it as the ID of a number.
Solution
Code
| The code is as follows: |
Copy code |
1 Sheets ("Sheet1"). Cells (I, 1). Value). Select
|
Change
| The code is as follows: |
Copy code |
1 Sheets ("Sheet1"). Cells (I, 1). text). Select
|
Note that the subsequent code will force the text type as the passed parameter type, and the problem can be solved.