[英]Sumproduct VBA returning a type mismatch error
我對 VBA 很陌生,所以我確信我缺少的東西非常簡單,而且有更多經驗的人不會忽視的東西。
我的代碼如下:
Sub test_match()
Sheets("Course Cascade").Select
Dim i As Long
Dim r As Long
Dim c As Long
Dim rfirst As Long
Dim cfirst As Long
Dim rlast As Long
Dim colnum As Long
Dim rownum As Long
Dim csum As Long
Dim rowTerm As Long
Dim k As Integer
Dim rabove As Long
Dim ccount As Long
Dim clkup As Long
r = 107: c = 7: rowTerm = 106: ccount = 3: cfirst = 7: csum = c - 1: k = WorksheetFunction.CountIf(Range("e107:e1661"), ">0"): clkup = 5
If Cells(r, ccount) <= WorksheetFunction.SumProduct((Range(Cells(47, c), Cells(74, c))), (--Range("CourseCascadeRow").Value = Cells(r, clkup))) Then
Cells(r, c) = WorksheetFunction.IfError(WorksheetFunction.Index("CourseCascadeCt", WorksheetFunction.Match(Range(Cells(r, 5)), Range("CourseCascadeCtRow"), 0), _
WorksheetFunction.Match(Range(Cells(106, c)), Range("CourseCascadeCol"), 0) / WorksheetFunction.Index("CourseCascadeCt", WorksheetFunction.Match(Range(Cells(r, 5)), Range("CourseCascadeCtRow"), 0), _
WorksheetFunction.Match(Range(Cells(106, c)), Range("CourseCascadeCol"), 0))), 0)
那里也有一個 else 參數,但我已經將類型不匹配錯誤隔離到 Sumproduct 公式的這一部分: --Range("CourseCascadeRow").Value = Cells(r, clkup))) 。 范圍末尾的“.value”是我嘗試添加和刪除而沒有更改的內容。
誰能告訴我我做錯了什么? 我知道代碼很難看...
謝謝!
順便說一句, @TimWilliams評論說,將其拆分並限制工作表函數的數量:
Sub kjlkjlkj()
Dim CCR As Variant
CCR = Range("CourseCascadeRow")
Dim vl As Double
vl = 0#
Dim i As Long
For i = 1 To UBound(CCR, 1)
Dim j As Long
For j = 1 To UBound(CCR, 2)
vl = vl + (Cells(47, c) * .Cells(74, c) * (-1 * CBool(CCR(i, j) = Cells(r, clkup))))
Next j
Next i
If .Cells(r, ccount) <= vl Then
Dim rw As Variant
rw = Application.Match(Range(Cells(r, 5)), Range("CourseCascadeCtRow"), 0)
Dim clm As Variant
clm = Application.Match(Range(Cells(106, c)), Range("CourseCascadeCol"), 0)
Cells(r, c.value = 0
If Not (IsError(rw) Or IsError(clm)) Then
Cells(r, c).Value = Range("CourseCascadeCt").Cells(rw, c).Value / Range("CourseCascadeCt").Cells(rw, c).Value
End If
End If
End Sub
這將有助於調試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.