簡體   English   中英

將Excel工作表功能轉換為VBA

[英]converting excel worksheet function into vba

我在使用以下宏時遇到問題。 我正在嘗試獲取以下功能,該功能可以在VBA中轉換。

=SUMPRODUCT(time!$I$1418:time!$I$39433,--(time!$E$1418:time!$E$39433=$B11),--(time!$G$1418:time!$G$39433=E$2))

這是因為如果該函數存在於太多的單元格中,它會使Excel的運行速度大大降低。

這是到目前為止我得到的。 我試圖從c列循環到第二行=“ total”的列,然后下一行,重復此過程,直到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

問題解決了。 我將工作表時間的名稱更改為time2,因為Excel已將時間用作特殊功能。 但是當我使用不再需要的評估功能時。

Evaluate(“ = SUMPRODUCT(Time!$ I $ 2:$ I $ 50000,-(Time!$ E $ 2:$ E $ 50000 =”&x&“),-(Time!$ G $ 2:$ G $ 50000 =” &y&“))”)

所以問題出在打字錯誤上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM