【发布时间】:2017-12-29 02:37:12
【问题描述】:
这是我第一次在 VBA 中使用数组。我试图根据特定条件检查我的数组的值。
我通过 Locals Window 检查我的数组值。窗户是空的。我做错了什么?
Option Explicit
Sub test()
'define dynamic array
Dim sn As Variant
Dim i As Long
'Loop through all the row
For i = 1 To Rows.Count
If Cells(i, 12).Value = "Renewal Reminder" And Not IsEmpty(Cells(i, 12).Value) Then
'assign cell value to array
sn = Cells(i, 1).Value
Debug.Print "aaa" ' there are 8 cell values that meet the condition
End If
Next i
End Sub
更新
Dim sn as Varient 以错误突出显示
用户定义类型未定义
【问题讨论】:
-
没有看到您的电子表格我不能肯定地说,但这里有一些想法。这是使用基于 1 或 0 的索引吗? (A1 列是“第 1 行第 1 列”还是“第 0 行第 0 列”?)您是否在 sn 或 Debug 行上设置了断点?一旦函数完成所需的几毫秒时间结束,监视窗口是否仍保持该值?
-
您没有使用
sn作为数组 - 您只是将单个单元格的值存储到变量中。 (FWIW,您的And Not IsEmpty(Cells(i, 12).Value)毫无意义,因为如果Cells(i, 12).Value = "Renewal Reminder"是True,那么您已经知道该单元格不是Empty。) -
@YowE3K 啊哈,你是对的。
-
您的编辑说您有一行写着
Dim sn as Varient,但这并没有出现在您发布的代码中。如果错误是正确的,你有一个错字。 -
@YowE3K 天哪,谢谢我把
Variant拼错了Varient