【发布时间】:2018-09-14 21:22:25
【问题描述】:
所以我的代码设置为发送一封电子邮件,其中包含符合特定单元格名称的所有行(缺少文本)。如果搜索中没有这些,我希望它绕过并输入“无”。如果有单元格有它,它工作得很好,但如果没有,我会得到一个下标超出范围错误。
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 是发生错误的时间。任何帮助都会很好,谢谢。
【问题讨论】:
-
为什么不测试
n>0? -
LOL 我想我是想让它变得更复杂,而且我已经盯着它看了一段时间。谢谢
-
是的,您可以检查是否
n > 0或使用布尔变量(如果您没有n),或使用this answer 中提到的函数如果您必须检查用于代码中许多地方的初始化数组。 -
是的,擦除后重新调整变体很好,但测试它是否有用是浪费时间。 IsArray 为真,IsMissing 和 IsEmpty 均为假。 LBound 和 UBound 都抛出错误。