我在使用VBA将函数插入Excel工作表时遇到问题。我以前从未做过这件事。有简单的方法吗?
我试过用
Range(r,c).Formula = "=FUNCTION()"
它适用于以下情况
"=SUM(C3:C10)"
但不是我的代码。我认为这是因为我在两者之间有一个字符串变量。
代码:
Sub populate()
Dim r As Long, r2 As Long, last_row As Long
Dim next_row As Long, current_len As Long, test_len As Long
Dim rng As String
With ActiveSheet
last_row = .Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To last_row
next_row = r + 1
If .Range("B" & next_row) > .Range("B" & r) Then
current_len = .Range("B" & r)
'create range
For r2 = r + 1 To last_row
test_len = .Range("B" & r2)
If current_len >= test_len Then
rng = "C" & r + 1 & ":" & "C" & r2 - 1
Exit For
End If
Next
.Range("C" & r) = "subtotal(9;" & rng & ")" '<-------
End If
Next
End With
End Sub