我一直在试用这个代码,如果我在公式中使用直接单元格引用它工作正常但是当我用单元格引用替换变量时它不起作用。
你能告诉我哪里出错了。
目的是将所有包含日期的单元格添加到11月
这是代码
Private Sub Worksheet_Change(ByVal Target As Range)
Dim last As Long
Dim cussat As Variant
Dim Cussatrange As String
With ActiveSheet
last = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
If Target.Address = "$C$" & last + 1 Then
Range("$B$" & last + 1).Value = Date
Range("$A$" & last + 1).Value = "Moss"
Cussatrange = "J1:J" & last
' I would like to substitue the cell reference in the above formula to use Cussatrange or last
cussat = [=SUMPRODUCT(--(TEXT(J1:J43,"mmm yyyy")="Nov 2014"))]
MsgBox "Last used row number in column A is " & last & " " & cussat & " " & Cussatrange
End If
End Sub
答案 0 :(得分:0)
如果我理解你的问题,那么你可以使用这样的东西:
Dim myFormula As String
Dim template As String
template = "=SUMPRODUCT(--(TEXT({0},""mmm yyyy"")=""Nov 2014""))"
myFormula = Replace(template, "{0}", Cussatrange)
cussat = Application.Evaluate(myFormula)
MsgBox "Last used row number in column A is " & last & " " & cussat & " " & Cussatrange
在代码中创建公式的模板,然后使用存储在字符串Cussatrange中的地址重新处理子字符串(在本例中为{0})。
有关Application.Evaluate方法的详细信息,请查看此处: http://msdn.microsoft.com/en-us/library/office/ff193019%28v=office.15%29.aspx。 (使用方括号(例如," [A1:C5]")与调用相同 带有字符串参数的Evaluate方法)