|
我想引用另一个表的数据来判断,可是得出的结果是错的,请高手指教,谢谢
Sub mymax()
Dim i%, j%, k%, arr, brr, d As Object
[r:r] = ""
brr = Sheet2.Range("c1:c" & Sheet2.[c65536].End(xlUp).Row)
arr = Sheet1.Range("a4:q" & [a65536].End(xlUp).Row)
With Sheet1
Set d = CreateObject("scripting.dictionary")
For i = 2 To UBound(arr)
d(arr(i, 4)) = d(arr(i, 4)) + arr(i, 7)
Next
For i = 2 To UBound(arr)
arr(i, 17) = d(arr(i, 4))
Next
.Range("q4").Resize(i, 1) = Application.Index(arr, 0, 17)
.Range("a4:q" & [a65536].End(xlUp).Row).Sort key1:=Range("g4"), Order1:=xlDescending, Header:=xlYes
.Range("a4:q" & [a65536].End(xlUp).Row).Sort key1:=Range("d4"), Order1:=xlAscending, Header:=xlYes
For i = 2 To UBound(arr)
j = i - 1
If Cells(i, 4) = Cells(j, 4) Then Cells(i, 17) = ""
Next
End With
For i = 5 To UBound(arr)
For k = 2 To UBound(brr)
If Sheet1.Cells(i, 6) <> "克" Then '如果 F 列单位不是“克”
a = 10 '则 a = 10
ElseIf arr(i, 4) = brr(k, 1) Then '否则如果 D 列名称 = sheet2.[C2:C4]中的名称 则执行
a = 10 'a = 10 这一步得出的是错误的结果
Else '否则
a = 1000 '否则 a = 1000
End If
mx = Application.RoundUp(Sheet1.Cells(i, 17) / a, 0) * a
Sheet1.Cells(i, 18) = Application.Max(mx, 0)
Next k, i
End Sub
|
|