我对vba还是新手。我有一个数据库,在其他人的帮助下,我最终能够在导入时进行验证检查。但是,我可以将数字作为文本进行检查,但如果我需要字母字符或者如果单元格为空白,我就会卡住。这就是我对数字作为文本所拥有的。我需要两个检查:1)接受字母数字或空白(null)和2)数字或空白(null)。
Function chk2(A As String) As Boolean
Dim i As Integer, l As Integer, c As String
l = Len(A)
If l = 4 Then
chk2 = True
For i = 1 To l
c = Mid(A, i, 1)
If Not (c >= "0" And c <= "9") Then
chk2 = False
Exit Function
End If
Next i
End If
End Function
只要在每行/单元格中填充字符,这个就可以正常工作。在此先感谢您的帮助。
如果你返回一个布尔值,你应该只返回一个真值,如果一切都正确执行,这样你就不会出现误报。您可以使用检查单元格是否为空白单元格的长度为0,即如果Len = 0您在If Not语句中执行的操作是检查c的ascii值是否在ascii值0和9之间,这样您就可以使用或语句检查它是否在a和z或A和Z之间,或者是否有一个空格是字符32 - Chr(32):
Function chk2(A As String) As Boolean
Dim i As Integer, l As Integer, c As String
chk2 = false
l = Len(A)
If l = 0 then
'do something if the cell is blank
chk2 = true
ElseIf l = 4 Then
For i = 1 To l
c = Mid(A, i, 1)
If Not ((c >= "0" And c <= "9") Or (c >= "a" And c <= "z") Or (c >= "A" And C <= "Z") Or c = Chr(32)) Then
Exit Function
End If
Next i
chk2=true
End If
End Function