我正在循环遍历WS中的单元格,并希望将单元格地址添加到范围(或数组)中,因为循环找到符合条件的单元格。我在最后一行Set
Dim CellArray As Range
With ws
With .Cells(Application.WorksheetFunction.Match("Total checks", .Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value,0))
.Formula = "=SUM('" & Root & sourceSheet & ws.Name & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$H:$H)"
Set CellArray = Union(CellArray, This.Address)
答案 0 :(得分:1)
您的CellArray
变量未初始化。因此,它最初为Nothing
,而Union
无法将Nothing
作为参数。
此外,您无法访问With对象(This
不存在),因此您必须首先影响Range到变量。
可以编写循环体(您必须事先声明Dim R As Range
):
Set R = Cells(Application.WorksheetFunction.Match("Total checks", .Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value,0))
R.Formula = "=SUM('" & Root & sourceSheet & ws.Name & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$H:$H)"
If CellArray Is Nothing Then
Set CellArray = R
Else
Set CellArray = Union(CellArray, R)
End If
答案 1 :(得分:0)
试试这个
请不要使用“With”命令,因为它会使代码在大多数情况下难以读取(例如你的)
require