mercredi 5 février 2020

VBA conditions for a UserForm

I am new on VBA. I want to put some restrictions on my inputs in the UserForm. I want My variable to be a number (the user should select the row Number) and if she/he put text or leave it blank an error message should appear. This is the code I have:

Public Sub AddPolicy_Click()
Dim RowNumber As Integer
RowNumber = TextBox1.Value
If RowNumber = "" Then
    MsgBox "Error Row Number- enter a value!", vbOKCancel + vbCritical, "Error"
ElseIf VarType(RowNumber) = vbString Then
    MsgBox "Error Row Number- enter a  Numerical Value!", vbOKCancel + vbCritical, "Error"
End If
Range("A1") = RowNumber
End Sub

I know that the problem is because I declare my variable RowNumber as an Integer, But if I do not do that the software does not recognize my variable, and show me the error message for text, blank and number. SO I do not know how can I solve this.

Thank you in advance for your help

Aucun commentaire:

Enregistrer un commentaire