Data types in Excel programming cause code running errors

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.