Saya perlu memberi tahu apakah varian elemen Array adalah angka sebagai string atau angka sebagai integer. Ketika saya menjalankan sub ini:

Public Sub arrTest1()

    Dim fldValues() As Variant

    fldValues = Array("1", 1)
        
    For i = LBound(fldValues) To UBound(fldValues)
        Debug.Print fldValues(i), IsNumeric(fldValues(i))
    Next i

End Sub

Saya mendapatkan hasil ini di jendela langsung:

1             True
 1            True

Karena lekukan saya berasumsi bahwa VBA tahu perbedaannya, tetapi keduanya tampak numerik. Bagaimana cara membedakan mereka?

1
Monardo 12 Mei 2021, 16:56

2 jawaban

Jawaban Terbaik

Salah satu opsinya adalah TypeName:

Debug.Print fldValues(i), TypeName(fldValues(i))

Keluaran:

1             String
 1            Integer

Opsi lain (lebih baik) adalah VarType; VarType(fldValues(i)) akan mengembalikan:

1              8 
 1             2 

Di mana 8 sesuai dengan vbString dan 2 sesuai dengan vbInteger.

3
BigBen 12 Mei 2021, 14:10

Solusi yang baik adalah perpustakaan yang kuat yang mencakup kecanggungan ini di VBA. Jadi ini adalah starter untuk 10.

Module Types

Option Explicit

Public Const NumberTypes As String = "Byte,Currency,Date,Decimal,Double,Integer,Long,LongLong,LongPtr,Single"
    

'@Description("Returns true if the variant is empty or contains an uninitialised array")
Public Function VarEmpty(ByVal ipVariant As Variant) As Boolean

    If IsArray(ipVariant) Then
    
        VarEmpty = UBound(ipVariant) = -1
        
    Else
    
        VarEmpty = IsEmpty(ipVariant)
        
    End If
    
End Function

'@Description("Sugar for 'Is Not x = nothing'")
Public Function IsSomething(ByVal ipValue As Object) As Boolean
    IsSomething = Not ipValue Is Nothing
End Function


Public Function IsBoolean(ParamArray ipArgs() As Variant) As Boolean
    IsBoolean = GetVarType(CVar(ipArgs), vbBoolean)
End Function

Public Function IsByte(ParamArray ipArgs() As Variant) As Boolean
    IsByte = GetVarType(CVar(ipArgs), vbByte)
End Function

Public Function IsCurrency(ParamArray ipArgs() As Variant) As Boolean
    IsCurrency = GetVarType(CVar(ipArgs), vbCurrency)
End Function

Public Function IsDataObject(ParamArray ipArgs() As Variant) As Boolean
    IsDataObject = GetVarType(CVar(ipArgs), vbDataObject)
End Function

Public Function IsDate(ParamArray ipArgs() As Variant) As Boolean
    IsDate = GetVarType(CVar(ipArgs), vbDate)
End Function

Public Function IsDecimal(ParamArray ipArgs() As Variant) As Boolean
    IsDecimal = GetVarType(CVar(ipArgs), vbDecimal)
End Function

Public Function IsDouble(ParamArray ipArgs() As Variant) As Boolean
    IsDouble = GetVarType(CVar(ipArgs), vbDouble)
End Function

Public Function IsInteger(ParamArray ipArgs() As Variant) As Boolean
    IsInteger = GetVarType(CVar(ipArgs), vbInteger)
End Function

Public Function IsLong(ParamArray ipArgs() As Variant) As Boolean
    IsLong = GetVarType(CVar(ipArgs), vbLong)
End Function

Public Function IsLongLong(ParamArray ipArgs() As Variant) As Boolean
    IsLongLong = GetVarType(CVar(ipArgs), vbLongLong)
End Function

'Public Function IsLongPtr(ParamArray ipArgs() As Variant) As Boolean
'    IsLongPtr = IsVarType(CVar(ipArgs), vbLongptr)
'End Function

Public Function IsSingle(ParamArray ipArgs() As Variant) As Boolean
    IsSingle = GetVarType(CVar(ipArgs), vbSingle)
End Function

Public Function IsString(ParamArray ipArgs() As Variant) As Boolean
    IsString = GetVarType(CVar(ipArgs), vbString)
End Function

Public Function IsUserDefinedType(ParamArray ipArgs() As Variant) As Boolean
    IsUserDefinedType = GetVarType(CVar(ipArgs), vbUserDefinedType)
End Function

'@Description("Returns true if the values in ipArgs() are of types listed in ipTypes")
Public Function IsOfType(ByVal ipTypes As String, ParamArray ipArgs() As Variant) As Boolean

    IsOfType = False
    
    Dim myItem As Variant
    For Each myItem In ipArgs
    
        If InStr(ipTypes, TypeName(myItem)) = 0 Then Exit Function
    
    Next
    
    IsOfType = True
    
End Function
    
Private Function GetVarType(ByVal ipArgs As Variant, ByVal ipVartype As VbVarType) As Boolean

    GetVarType = False
    
    Dim myItem As Variant
    For Each myItem In ipArgs
    
        If Not VarType(myItem) = ipVartype Then Exit Function
        
    Next
    
    GetVarType = True

End Function


End Module

Jadi kode Anda akan menjadi

Public Sub arrTest1()

    Dim fldValues() As Variant

    fldValues = Array("1", 1)
        
    For i = LBound(fldValues) To UBound(fldValues)
        Debug.Print fldValues(i), IsOfType(NumberType,fldValues(i))
        'Or 
        'Debug.Print fldvalues(i),IsInteger(fldValues(i)
    Next i

End Sub

0
freeflow 12 Mei 2021, 15:15