为什么它会给我一个不匹配的错误?

时间:2014-01-03 17:04:48

标签: excel-vba runtime-error vba excel

当提示用户提供我的excel文档时,我让他们输入1到10之间的数字。子测试是消除1到10之外的字符串和数字。这个子工作。如果用户第一次输入数字1到10,则该程序就像魅力一样。但是,如果用户首先输入字符串然后输入1-10的整数,则会弹出一个错误。这是运行时错误13类型不匹配。我已经将弹出的行注释为错误。但是,电子邮件仍然存在。另外,如果你能解释为什么我必须重新定义我的RANK整数,那也会有所帮助。选项明确不像我在书中读到的那样有效。谢谢!如果您有任何疑问,请告诉我!!

Option Explicit
Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim RANK As Long


Call Test

RANK = UserForm1.TextBox1.Value  '((((THIS IS THE LINE THAT PROMPTS THE ERROR)))
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "(my email is inserted here)"
.CC = ""
.BCC = ""
.Subject = "Create Database Overall Ranking"
.Body = "The rank a company gave me was a " & RANK & "."

.send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

Unload UserForm1

End Sub

Sub Test()
Dim MyVar, MyCheck
Dim RANK As Long
 MyVar = UserForm1.TextBox1.Text ' Assign value.
 MyCheck = IsNumeric(MyVar) ' Returns True.

If MyCheck = True Then
RANK = UserForm1.TextBox1.Value

If RANK > 10 Or RANK < 1 Then

MsgBox "Please input a number between 1 and 10.", vbOKOnly, "Invalid Number"
UserForm1.TextBox1.Text = ""
On Error Resume Next
Unload UserForm1
UserForm1.Show
Else
End If
Else
MsgBox "Please input a number and not text.", vbOKOnly, "Invalid Input"
UserForm1.TextBox1.Text = ""
On Error Resume Next
Unload UserForm1
UserForm1.Show
End If
End Sub

编辑:工作的代码!!!

我让它工作....这是代码。我只是进行了测试,以确保它是一个数字,我将数字作为字符串运行。

Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim SurveyNum As String


Call Test

SurveyNum = UserForm1.TextBox1.Text

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)


On Error Resume Next
With OutMail
.To = "(MY EMAIL HERE)"
.CC = ""
.BCC = ""
.Subject = "Create Database Overall Ranking"
.Body = "The rank a company gave me was a " & SurveyNum & "."

.send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

Unload UserForm1

End Sub
Sub Test()
Dim MyVar, MyCheck
Dim SurveyNum2 As Double
 MyVar = UserForm1.TextBox1.Text ' Assign value.
 MyCheck = IsNumeric(MyVar) ' Returns True.

If MyCheck = True Then
SurveyNum2 = UserForm1.TextBox1.Value

If SurveyNum2 > 10 Or SurveyNum2 < 1 Then

MsgBox "Please input a number between 1 and 10.", vbOKOnly, "Invalid Number"
UserForm1.TextBox1.Text = ""
On Error Resume Next
Unload UserForm1
UserForm1.Show
Else
End If
Else
MsgBox "Please input a number and not text.", vbOKOnly, "Invalid Input"
UserForm1.TextBox1.Text = ""
On Error Resume Next
Unload UserForm1
UserForm1.Show
End If
End Sub

1 个答案:

答案 0 :(得分:0)

有一些事情可能导致问题。

Rank是VBA中已存在的函数。对于用户定义的内容,您可能会遇到使用相同名称的问题。虽然我认为这不会给你带来错误。

我认为问题在于声明RANK的时间长。据我所知,long数据类型是一个整数。如果传递给此数据类型的值不是整数,则可能是罪魁祸首。

我会重命名rank变量并将其重新定义为double,看看它为你做了什么。

使用代码进行编辑

我设法修复了代码。我不确切知道是什么导致了这个问题,但我改变了一些项目。

  1. 您在相同的教科书值的相同过程中声明了单独的变量,但是使用了不同的数据类型(RANK为Long,myVar为测试过程中的Variant)
  2. 我还清理了一些项目以使代码流程更好。
  3. 这是代码。

    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim SurveyNum As Double
    
        If Test = True Then
    
            SurveyNum = UserForm1.TextBox1.Value
            Set OutApp = CreateObject("Outlook.Application")
            OutApp.Session.Logon
            Set OutMail = OutApp.CreateItem(0)
    
            On Error Resume Next
            With OutMail
                .To = "(my email is inserted here)"
                .CC = ""
                .BCC = ""
                .Subject = "Create Database Overall Ranking"
                .Body = "The rank a company gave me was a " & SurveyNum & "."
    
                .send
            End With
            On Error GoTo 0
    
            Set OutMail = Nothing
            Set OutApp = Nothing
    
            Unload UserForm1
        End If
    End Sub
    
    Function Test() As Boolean
        Dim MyVar As Double
        Dim MyCheck As Boolean
    
        MyVar = UserForm1.TextBox1.Value ' Assign value.
        MyCheck = IsNumeric(MyVar) ' Returns True.
    
        If MyCheck = True Then
    
            If MyVar > 10 Or MyVar < 1 Then
    
                MsgBox "Please input a number between 1 and 10.", vbOKOnly, "Invalid Number"
                UserForm1.TextBox1.Value = ""
                On Error Resume Next
                Test = False
            Else
                Test = True
            End If
        Else
            MsgBox "Please input a number and not text.", vbOKOnly, "Invalid Input"
            UserForm1.TextBox1.Value = ""
            On Error Resume Next
            Test = False
        End If
    End Function