简体   繁体   English

在 Excel VBA 中设置默认的 TextBox.Value TypeName

[英]Setting the Default TextBox.Value TypeName in Excel VBA

I have a vba function that checks if a user-input from a text box is positive integer or not.我有一个 vba 函数,用于检查文本框中的用户输入是否为正整数。 The code below:下面的代码:

Public Function IsPosInteger(n As Variant) As Boolean  
    If IsNumeric(n) = True Then
        If (n = Int(n)) And n > 0 Then
            IsPosInteger = True
        Else
            IsPosInteger = False
        End If
    Else
        IsPosInteger = False
End If
End Function

The problem is that upon testing the function still returns false for a valid positive integer.问题是,在测试时,该函数仍然为有效的正整数返回 false。 Upon further investigation, I noticed that the variable type by default for texbox values is String.经过进一步调查,我注意到默认情况下 texbox 值的变量类型是字符串。 Probably the main reason why IsNumeric is returning false.可能是 IsNumeric 返回 false 的主要原因。

Function below is what I used to determine the type of the variable.下面的函数是我用来确定变量类型的。

TypeName(n)

This worked for me.这对我有用。

I used an inputbox that contained:我使用了一个包含以下内容的输入框:

  • strings (False)字符串(错误)
  • negative numbers (False)负数 (False)
  • positive numbers (true)正数(真)
  • positive numbers and letters (false)正数和字母 (false)

Public Function IsPosInteger(n As Variant) As Boolean
    If n > 0 And IsNumeric(n) Then IsPosInteger = True
End Function

Now, the other issue may arise that the value n is still technically type String due to the nature of the inputbox -- even if it passes the test of this function.现在,可能会出现另一个问题,即由于输入框的性质,值n在技术上仍然是String类型——即使它通过了此函数的测试。 If you wish to change this behavior , continue reading.如果您希望改变这种行为,请继续阅读。

To do this, ensure that you are using ByRef (when this is intentional , I usually type out ByRef on the argument, even though it is automatically assumed by VBA that any argument passed to a function is ByRef if it doesn't explicitly state ByVal ) .为此,请确保您使用的是ByRef (如果这是有意的,我通常会在参数上键入ByRef ,即使 VBA 自动假定传递给函数的任何参数都是ByRef ,如果它没有明确声明ByVal )

If this is the outcome you are wanting, then you can use this function:如果这是您想要的结果,那么您可以使用此功能:

Public Function IsPosInteger(ByRef n As Variant) As Boolean
    If n > 0 And IsNumeric(n) Then 
        IsPosInteger = True
        n = clng(n) '<-- This converts the variant (currently a string) n to type long,
                    '    only if it passes the test
    end if
End Function

You must ensure that n in the calling routine is of type Variant , else you will encounter an error.您必须确保调用例程中的nVariant类型,否则您将遇到错误。

Public Function IsPosInteger(s As String) As Boolean 'boolean data type is false by default.
    If (IsNumeric(s) = False) Then Exit Function
    If (s < 1) Then Exit Function
    If (InStr(s, ".") = False) Then IsPosInteger = True
End Function

Function tests that the input is numeric, not less than 1, and does not contain a decimal.函数测试输入是否为数字,不小于 1,并且不包含小数。 Here is an example of how you could use it in your calling sub:以下是如何在调用子中使用它的示例:

Sub TestInput()
    Dim sUserInput As String
    Dim boolPositiveInt As Boolean
    Dim i As Integer

    sUserInput = Range("A1").Value2
    boolPositiveInt = IsPosInteger(sUserInput)
    If (boolPositiveInt = False) Then
        MsgBox "Invalid input. Please enter a positive integer"
        Exit Sub
    End If

    i = CInt(sUserInput)
    'continue working with integer variable here
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM