[英]Validating user input excel vba
I have the following user input set up in excel vba. 我在excel vba中设置了以下用户输入。 The function when called asks the user to input a single number no greater than 9999, or two numbers in the format XXXX-XXXX where two numbers are separated by a dash.
调用该函数时,要求用户输入不大于9999的单个数字,或者输入格式为XXXX-XXXX的两个数字,其中两个数字之间用短划线隔开。 In that case, the numbers cannot be greater than 9999 in either case, or at least shouldn't.
在那种情况下,无论哪种情况,数字都不能大于9999,或者至少不应该大于9999。
The goal is to return either a single number (IE 50) or a range (IE low value is 50,high value is 75). 目标是返回单个数字(IE 50)或范围(IE低值是50,高值是75)。 Currently as set up it should be returning an array, where the first position is the low value, and the second position is high value.
当前,它应该返回一个数组,其中第一个位置是低值,第二个位置是高值。 Or, if the user only enters one number, it should return that one number in the first position of an array.
或者,如果用户仅输入一个数字,则应在数组的第一个位置返回该数字。
Currently it checks that A) user has entered in a number, B) the number is not greater than 4 digits long. 当前,它检查A)用户是否输入了数字,B)该数字不超过4位数字。
Unfortunately it is not returning an array, it is returning an error. 不幸的是,它没有返回数组,而是返回了错误。 Subscript out of range.
下标超出范围。
Also, are there any other likely user inputs that should be checked for here? 另外,是否还有其他可能的用户输入应在此处进行检查? The application is not going to be widely used by people, but I'd like to minimize potential errors as well.
该应用程序不会被人们广泛使用,但是我也想将潜在的错误减到最少。
Public Function getUserInput() As Variant
'this function gets a user input from an input box and puts it out into the proper format
Dim inputString As String
Dim numArr() As String
Dim i As Long
' On Error GoTo NotValidInput
inputString = Trim(InputBox("Enter the rows you'd like to print"))
'has the user entered a dash into their user input
If InStr(inputString, "-") > 0 Then
numArr() = Split(inputString, "-")
If UBound(numArr) <> 1 Then
GoTo NotValidNumberFormat
End If
If (IsNumeric(numArr(0)) And Len(numArr(0)) <= 4) And (IsNumeric(numArr(1)) And Len(numArr(1)) <= 4) Then
getUserInput = numArr
Exit Function
Else
GoTo NotValidNumberFormat
End If
'no dash
'60
Else
If (IsNumeric(CInt(inputString))) And Len(inputString) <= 4 Then
getUserInput = numArr
Exit Function
Else
GoTo NotValidNumberFormat
End If
End If
Exit Function
NotValidNumberFormat:
'if the conversion failed, return error
MsgBox ("Please enter the number in a valid format - either a single number no larger than 9999 or two numbers no larger than 9999 separated by only one dash (IE XX-XX)")
getUserInput = -1
End Function
this should do: 这应该做:
Public Function getUserInput() As Variant
'this function gets a user input from an input box and puts it out into the proper format
Dim numArr As Variant
Dim goOn As Boolean
Do
numArr = Split(WorksheetFunction.Trim(InputBox("Enter the rows you'd like to print in the format 'nnnn' or 'nnnn-mmmm'")), "-")
Select Case UBound(numArr)
Case 0
goOn = Format(numArr(0), "0000") Like "####"
Case 1
goOn = Format(numArr(0), "0000") Like "####" And Format(numArr(1), "0000") Like "####"
End Select
If Not goOn Then MsgBox "Please enter the number in a valid format - either a single number no larger than 9999 or two numbers no larger than 9999 separated by only one dash (ex: XX-XX)"
Loop While Not goOn
getUserInput = numArr
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.