Arrays 检查数组是否有值,如果为空则忽略
因此,我的代码设置为发送一封电子邮件,其中包含符合某个单元格名称的所有行(缺少文本)。如果搜索中没有这些,我希望它绕过并输入“无”。如果有细胞有它,它的工作原理很好,但如果没有,我会得到一个下标o范围外的错误Arrays 检查数组是否有值,如果为空则忽略,arrays,excel,vba,Arrays,Excel,Vba,因此,我的代码设置为发送一封电子邮件,其中包含符合某个单元格名称的所有行(缺少文本)。如果搜索中没有这些,我希望它绕过并输入“无”。如果有细胞有它,它的工作原理很好,但如果没有,我会得到一个下标o范围外的错误 Dim MissingText() As Variant Dim WrongNum() As Variant Dim BlankText() As Variant Dim objOutlook As Object Dim objMsg As Object Set objOutlook
Dim MissingText() As Variant
Dim WrongNum() As Variant
Dim BlankText() As Variant
Dim objOutlook As Object
Dim objMsg As Object
Set objOutlook = CreateObject("Outlook.Application")
Erase MissingText, WrongNum, BlankText
Listed = 0
Ending = Cells(Rows.Count, 5).End(xlUp).Row
n = 0
For Listed = 2 To Ending
If Cells(Listed, 10).Value = "Missing Text" Then
ReDim Preserve MissingText(n)
MissingText(n) = Listed
n = n + 1
End If
Next Listed
If IsEmpty(MissingText) Then
MissingTogether = "None"
GoTo MissingSkip
End If
CountArray = UBound(MissingText, 1) - LBound(MissingText, 1) + 1
CountArray = CountArray - 1
MissingTogether = Join(MissingText, ", ")
MissingSkip:
(续)
在CountArray=UBound(MissingText,1)-LBound(MissingText,1)+1是发生错误的时间。任何帮助都会很好,谢谢。我将使用字符串变量和
split()
dim strMissing as string, aryMissing as variant
For Listed = 2 To Ending
If Cells(Listed, 10).Value = "Missing Text" Then
strMissing = Listed & ", " & strMissing
End If
Next Listed
If strMissing = "" then
MissingTogether = "None"
GoTo MissingSkip
else
aryMissing = split(strMissing, ", ")
CountArray = UBound(MissingText, 1) - LBound(MissingText, 1) + 1
End If
正如注释中指出的,没有一种本机方法来确定数组是否在VBA中未初始化。但是,您可以检查它的内存占用,看看它的变量是否包含空指针。请注意,VarPtr
引发数组的类型不匹配,因此需要首先将其包装在变量中:
'In declarations section:
#If VBA7 Then
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, _
ByVal length As Long)
#Else
Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, _
ByVal length As Long)
#End If
Private Const VT_BY_REF As Integer = &H4000&
Private Const DATA_OFFSET As Long = 8
Private Function IsUninitializedArray(SafeArray As Variant) As Boolean
If Not IsArray(SafeArray) Then
Exit Function
End If
Dim vtype As Integer
'First 2 bytes are the VARENUM.
CopyMemory vtype, SafeArray, LenB(vtype)
#If VBA7 Then
Dim lp As LongPtr
#Else
Dim lp As Long
#End If
'Get the data pointer.
CopyMemory lp, ByVal VarPtr(SafeArray) + DATA_OFFSET, LenB(lp)
'Make sure the VARENUM is a pointer.
If (vtype And VT_BY_REF) <> 0 Then
'Dereference it for the actual data address.
CopyMemory lp, ByVal lp, LenB(lp)
IsUninitializedArray = lp <> 0
End If
End Function
为什么不测试n>0
?哈哈,我想我是想把它弄得更复杂,而且我已经盯着它看了一段时间了。谢谢你是的,你可以检查n>0
或者使用一个布尔变量(在你没有n
的情况下),或者使用中提到的函数,如果你必须在你的代码中的很多地方检查初始化的数组。是的,擦除后重新定义一个变量是可以的,但是测试它是否有用是浪费时间。IsArray是真的,IsMissing和IsEmpty都是假的。LBound和UBound都会抛出错误。
Public Sub Example()
Dim Test() As String
MsgBox IsUninitializedArray(Test) 'False
Test = Split(vbNullString)
MsgBox IsUninitializedArray(Test) 'True
Erase Test
MsgBox IsUninitializedArray(Test) 'False
End Sub