我遇到以下宏问题。我正在尝试获得以下在VBA中转换的函数。
=SUMPRODUCT(time!$I$1418:time!$I$39433,--(time!$E$1418:time!$E$39433=$B11),--(time!$G$1418:time!$G$39433=E$2))
这是因为如果Excel存在于太多单元格中,该函数会使Excel减慢太多。
这是我到目前为止所得到的。我试图从列c循环到第二行=“总计”的列,然后我往下一行继续我重复该过程,直到列B中的单元格为空。
我遇到了运行时错误1004,应用程序定义错误或对象定义错误,他们突出显示了sumproduct部分。
Sub date_stats()
Dim first_cell As Integer
Dim time As Worksheet
Set time = ActiveWorkbook.Sheets("time")
Dim i As Integer, j As Integer, num As Integer
firstcell = Range("e65000").End(xlUp).Row + 1
Do Until ActiveSheet.Range("b" & firstcell).Value = ""
firstcell = firstcell + 1
i = 2
Do Until ActiveSheet.Cells(2, i).Value = "total"
i = i + 1
num = Application.WorksheetFunction.SumProduct(time.Range("i2:i50000"), --(time.Range("E2:E50000") = ActiveSheet.Range("B" & first_cell)), --(time.Range("G2:G50000") = ActiveSheet.Cells(i, 2)))
Loop
Loop
第一次更新
我正在取得进步。我忘了我正在使用一个不同的模块,因此没有明确放在新模块上的选项,而且我拼错了firstcell和first_cell。我已经在VBA上删除了所有错误,但现在结果是#REF,这意味着它正在引用已删除的单元格,显然不是这种情况。这是新的更新工作簿。
https://drive.google.com/file/d/0B9zzW6-3m2qGeFNCdEhtem44Wnc/edit?usp=sharing
以下是代码:
Sub date_stats()
Dim first_cell As Integer
Dim time2 As Worksheet
Set time2 = ActiveWorkbook.Sheets("time")
Dim i As Integer, j As Integer, num As Integer
Dim x As String
Dim y As String
Dim z As Range
'num = Format(num, "standard")
Application.ScreenUpdating = False
With ActiveSheet
first_cell = .Range("e65000").End(xlUp).Row
'=SUMPRODUCT(time!$I$1418:time!$I$39433,--(time!$E$1418:time!$E$39433=$B11),--(time!$G$1418:time!$G$39433=E$2))
Do Until .Range("b" & first_cell).Value = ""
first_cell = first_cell + 1
i = 3
Do Until .Cells(2, i).Value = "total"
x = .Range("B" & first_cell).Address
y = .Cells(2, i).Address
Set z = .Cells(first_cell, i)
z.Value = Evaluate("=SUMPRODUCT(time2!$I$2:$I$50000,--(time2!$E$2:time2!$E$50000=" & x & "),--(time2!$G$2:time2!$G$50000=" & y & "))")
i = i + 1
Loop
Loop
End With
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
问题解决了。我将工作表时间的名称更改为time2,因为Excel已将时间用作特殊功能。但是当我使用不再需要的评估功能时。
评估(“= SUMPRODUCT(时间!$ I $ 2:$ I $ 50000, - (时间!$ E $ 2:$ E $ 50000 =”& x&“), - (时间!$ G $ 2: $ G $ 50000 =“& y&”)))“)
所以这个问题是由于一个错字。