检查数组元素是否等于 Space 时出现意外的类型不匹配错误
Unexpected Type Mismatch Error when Checking If Array Element Equals Space
为清楚起见,我在下面发布了我的全部代码及其使用的所有函数。此宏的目标是读取如下所示的数据:XY05-09 XY37-49 并根据单元格中的范围生成标签。在这种情况下,它会生成 XY05、XY06、XY07 等。我的问题是行
If ArrOfChar(element) = " " Then
其中 returns 类型不匹配。我已经使用 MSgBox 在创建每个元素时输出它们,它看起来将 space 存储为一个元素,但出于某种原因我无法将元素的值与“”进行比较。我已经研究了一段时间,但没有找到关于为什么会这样的解释。感谢您的帮助!
Sub Label_Generator_Button1_Click()
Dim InitialString As String
Dim Prefix As String
Dim FirstNum As Integer
Dim LastNum As Integer
Dim IsLastNum As Boolean
Dim InsertZero As Boolean
Dim ArrOfChar() As String
Prefix = ""
FirstNum = 0
LastNum = 0
IsLastNum = False
InsertZero = False
InitialString = Range("A1")
ReDim ArrOfChar(Len(InitialString) - 1)
For i = 1 To Len(InitialString)
ArrOfChar(i - 1) = Mid$(InitialString, i, 1)
'MsgBox ArrOfChar(i - 1)
Next
For Each element In ArrOfChar
If ArrOfChar(element) = " " Then 'If space, reset all to zero, new number WHY IS THIS TYPE MISMATCH
For i = FirstNum To LastNum
Range("B2").Value = Range("B2").Value & Prefix
If InsertZero = True And i < 10 Then
Range("B2").Value = Range("B2").Value & "0"
End If
Range("B2").Value = Range("B2").Value & i & " "
Next i
Prefix = ""
FirstNum = 0
LastNum = 0
InsertZeroFirst = False
InsertZeroLast = False
IsLastNum = False
GoTo NextIteration
End If
If ArrOfChar(element) = "-" Then 'If a dash is used, flag the system to switch to last number and make insert zero false
IsLastNum = True
GoTo NextIteration
End If
If IsLetters(ArrOfChar(element)) = True Then 'If is a letter add to prefix
Prefix = Prefix & ArrOfChar(element)
GoTo NextIteration
End If
If ArrOfChar(element) = "0" And FirstNum = 0 Then ' If is 0 and the first number, set flag to insert leading 0
InsertZero = True
GoTo NextIteration
End If
If IsNumbers(ArrOfChar(element)) = True Then
If IsLastNum = False Then
FirstNum = CInt(ArrOfChar(element))
GoTo NextIteration
End If
If IsLastNum = True Then
LastNum = CInt(ArrOfChar(element))
GoTo NextIteration
End If
End If
If ArrOfChar(element) = "0" And FirstNum > 0 Then 'If is 0 and not the first number, multiply first number by 10. If we are given a 5 then the next is zero, multuplying by 10 gives us our aditional place
FirstNum = FirstNum * 10
GoTo NextIteration
End If
NextIteration:
Next element
End Sub
Function IsLetters(Str As String) As Boolean
Dim i As Integer
For i = 1 To Len(Str)
Select Case Asc(Mid(Str, i, 1))
Case 65 To 90, 97 To 122
IsLetters = True
Case Else
IsLetters = False
Exit For
End Select
Next i
End Function
Function IsNumbers(Str As String) As Boolean
Dim i As Integer
For i = 1 To Len(Str)
Select Case Asc(Mid(Str, i, 1))
Case 49 To 57
IsNumbers = True
Case Else
IsNumbers = False
Exit For
End Select
Next i
End Function
因为 For Each element In ArrOfChar
你的 element
IS 是数组的元素本身而不是 index一个元素。
因此 ArrOfChar(element - 1)
失败,因为 element
不是数字!
使用 element = " "
访问元素或将循环更改为索引循环,例如:
Dim idx As Long
For idx = LBound(ArrOfChar) To UBound(ArrOfChar)
Debug.Print idx, ArrOfChar(idx) ' print idx and value of the element
Next idx
这将从第一个元素循环到最后一个元素,idx
是元素的索引。
确保使用 Option Explicit
并正确声明所有变量。我建议始终激活 Option Explicit
:在 VBA 编辑器中转到 Tools › Options › Require Variable Declaration.
您收到错误的原因是因为 element
是一个字符,而不是一个整数。 VBA 无法评估 element - 1
要遍历,您需要将元素更改为类似这样的内容。
For element = 1 To Len(InitialString)
为清楚起见,我在下面发布了我的全部代码及其使用的所有函数。此宏的目标是读取如下所示的数据:XY05-09 XY37-49 并根据单元格中的范围生成标签。在这种情况下,它会生成 XY05、XY06、XY07 等。我的问题是行
If ArrOfChar(element) = " " Then
其中 returns 类型不匹配。我已经使用 MSgBox 在创建每个元素时输出它们,它看起来将 space 存储为一个元素,但出于某种原因我无法将元素的值与“”进行比较。我已经研究了一段时间,但没有找到关于为什么会这样的解释。感谢您的帮助!
Sub Label_Generator_Button1_Click()
Dim InitialString As String
Dim Prefix As String
Dim FirstNum As Integer
Dim LastNum As Integer
Dim IsLastNum As Boolean
Dim InsertZero As Boolean
Dim ArrOfChar() As String
Prefix = ""
FirstNum = 0
LastNum = 0
IsLastNum = False
InsertZero = False
InitialString = Range("A1")
ReDim ArrOfChar(Len(InitialString) - 1)
For i = 1 To Len(InitialString)
ArrOfChar(i - 1) = Mid$(InitialString, i, 1)
'MsgBox ArrOfChar(i - 1)
Next
For Each element In ArrOfChar
If ArrOfChar(element) = " " Then 'If space, reset all to zero, new number WHY IS THIS TYPE MISMATCH
For i = FirstNum To LastNum
Range("B2").Value = Range("B2").Value & Prefix
If InsertZero = True And i < 10 Then
Range("B2").Value = Range("B2").Value & "0"
End If
Range("B2").Value = Range("B2").Value & i & " "
Next i
Prefix = ""
FirstNum = 0
LastNum = 0
InsertZeroFirst = False
InsertZeroLast = False
IsLastNum = False
GoTo NextIteration
End If
If ArrOfChar(element) = "-" Then 'If a dash is used, flag the system to switch to last number and make insert zero false
IsLastNum = True
GoTo NextIteration
End If
If IsLetters(ArrOfChar(element)) = True Then 'If is a letter add to prefix
Prefix = Prefix & ArrOfChar(element)
GoTo NextIteration
End If
If ArrOfChar(element) = "0" And FirstNum = 0 Then ' If is 0 and the first number, set flag to insert leading 0
InsertZero = True
GoTo NextIteration
End If
If IsNumbers(ArrOfChar(element)) = True Then
If IsLastNum = False Then
FirstNum = CInt(ArrOfChar(element))
GoTo NextIteration
End If
If IsLastNum = True Then
LastNum = CInt(ArrOfChar(element))
GoTo NextIteration
End If
End If
If ArrOfChar(element) = "0" And FirstNum > 0 Then 'If is 0 and not the first number, multiply first number by 10. If we are given a 5 then the next is zero, multuplying by 10 gives us our aditional place
FirstNum = FirstNum * 10
GoTo NextIteration
End If
NextIteration:
Next element
End Sub
Function IsLetters(Str As String) As Boolean
Dim i As Integer
For i = 1 To Len(Str)
Select Case Asc(Mid(Str, i, 1))
Case 65 To 90, 97 To 122
IsLetters = True
Case Else
IsLetters = False
Exit For
End Select
Next i
End Function
Function IsNumbers(Str As String) As Boolean
Dim i As Integer
For i = 1 To Len(Str)
Select Case Asc(Mid(Str, i, 1))
Case 49 To 57
IsNumbers = True
Case Else
IsNumbers = False
Exit For
End Select
Next i
End Function
因为 For Each element In ArrOfChar
你的 element
IS 是数组的元素本身而不是 index一个元素。
因此 ArrOfChar(element - 1)
失败,因为 element
不是数字!
使用 element = " "
访问元素或将循环更改为索引循环,例如:
Dim idx As Long
For idx = LBound(ArrOfChar) To UBound(ArrOfChar)
Debug.Print idx, ArrOfChar(idx) ' print idx and value of the element
Next idx
这将从第一个元素循环到最后一个元素,idx
是元素的索引。
确保使用 Option Explicit
并正确声明所有变量。我建议始终激活 Option Explicit
:在 VBA 编辑器中转到 Tools › Options › Require Variable Declaration.
您收到错误的原因是因为 element
是一个字符,而不是一个整数。 VBA 无法评估 element - 1
要遍历,您需要将元素更改为类似这样的内容。
For element = 1 To Len(InitialString)