When the user is prompted to give a survery of my excel document, I have them enter in a number between 1 and 10. The Sub Test is to eliminate Strings and numbers outside of 1 through 10. This sub works. If the user enters a number 1 through 10 the first time, the program works like a charm. However, there is an error that pops up if the user enters a string first and then puts in an integer from 1-10. It is a run-time error 13 type mismatch. I have commented out the line that pops up as an error. However, the email goes through still. Also, if you can explain why I have to redefine my RANK integer, that would also help. Option Explicit is not working like I read about in my book. Thank you! If you have any questions, please let me know!!
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
EDIT: THE CODE THAT WORKS!!!
I got it to work....here is the code. I just had the test to make sure that it is a number and i ran the number as a string.
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