VBA在看似任意的时间吐出错误?
我在第2行的所有相关列中填写了一系列公式。之后,我有了用动态引用填充范围的代码。这个特定的数据集有74行数据,但由于某种原因 438错误:“对象不支持此属性或方法” 每次都在第65排VBA在看似任意的时间吐出错误?,vba,excel,Vba,Excel,我在第2行的所有相关列中填写了一系列公式。之后,我有了用动态引用填充范围的代码。这个特定的数据集有74行数据,但由于某种原因 438错误:“对象不支持此属性或方法” 每次都在第65排 'Find last row/col variables Dim lastRow As Long Dim lastCol As Long 'Array variable Dim TestFormulas() As Variant
'Find last row/col variables
Dim lastRow As Long
Dim lastCol As Long
'Array variable
Dim TestFormulas() As Variant
'WS variable
Dim WS as Worksheet
lastRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
lastCol = Cells.Find(What:="*", _
After:=Range("A1"), LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'Create Array [this is cut down significantly. There are roughly 50 formulas here. Code runs quickly, if it matters.]
TestFormulas() = Array( _
"=Sheet1!H2", "=Sheet1!B2", "=IF(Sheet1!P2=""Resiliency""")
'Fill Row 2 with formulas
With WS
For i = LBound(TestFormulas()) To UBound(TestFormulas())
.Cells(2, 1 + i).Formula = TestFormulas(i)
Next i
End With
'Copy formulas and fill down the entire range
Range("A2:" & lastCol & ":" & "2").Formula = TestFormulas
Range("A2:" & WS(1).lastCol & ":" & WS(1).lastRow).FillDown
我希望这个范围能充满动态公式。它在动态上填充,尽管只填充到并包括第65行
438错误:“对象不支持此属性或方法”
Range(“A2:&lastCol&“:”&“2”)
lastCol是一个数字,因此它将连接到A2:52:2
,该地址不是有效的范围地址。您需要在Range对象内使用接受数字列的单元格()
。您确定可以执行…公式=[公式数组]
?你可能需要循环一下。嘿,斯科特!我只能用这段代码填充整个第1行。如果有任何不同,我已经将其设置为创建和激活新工作表,因此我没有使用set WS=Worksheets(“Sheet4”)行。那么您是在另一个工作表中查找最后一行吗?看编辑。啊,好的。我明白我的错误了。它只考虑了那1行数据,然后在此基础上填充,而没有考虑上一页上的数据。谢谢你,先生!
Dim lastRow As Long
'Array variable
Dim TestFormulas() As Variant
'WS variable
Dim WS As Worksheet
Set WS = Worksheets("Sheet4") ' change to your sheet name
With WS
If Application.WorksheetFunction.CountA(Worksheets("Sheet1").Cells) > 0 Then
lastRow = Worksheets("Sheet1").Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'Create Array [this is cut down significantly. There are roughly 50 formulas here. Code runs quickly, if it matters.]
TestFormulas() = Array( _
"=Sheet1!H2", "=Sheet1!B2", "=IF(Sheet1!P2=""Resiliency"",TRUE,FALSE)")
'Fill Row 2 with formulas
For i = LBound(TestFormulas()) To UBound(TestFormulas())
.Range(.Cells(2, 1 + i), .Cells(lastRow, 1 + i)).Formula = TestFormulas(i)
Next i
End If
End With