如何在 VBA 中创建一个循环,在满足条件时执行 Sumproduct 操作
How to make a Loop in VBA that do an operation of Sumproduct when a condition is met
我想做一个循环,对用 Id 标识的每个组执行 SumProduct 操作。我的意思是我需要在第 2 列和第 3 列之间进行 SumProduct 操作,并且结果出现在该列的空单元格中。我已经尝试使用下一个代码,但它没有像我期望的那样工作。我曾尝试更改范围内的 Starpoint,但我的解决方案没有奏效。感谢您给我的反馈和帮助。
Startrow = 2
Lastpoint = Cells(Rows.Count, 3).End(xlUp).Row
For i = Startrow To Lastpoint + 1
If Cells(i, 3).Value = "" Then Cells(i, 3).Value = Application.WorksheetFunction.SumProduct(a, b)
a = Range("C2: C" & Cells(i, 3).End(xlDown).Row)
b = Range("B2: B" & Cells(i, 3).End(xlDown).Row)
Next i
您需要在代码中实际定义范围 a,b
。试试这个。
Option Explicit 'pay attention and read up on this. crucial to developing bug free code.
Sub test()
Dim startRow as Long, lastPoint as Long 'always, always declare variables and the type
startRow = 2
lastpoint = Cells(Rows.Count, 3).End(xlUp).Row
For i = Startrow To Lastpoint + 1
Dim a as Range, b as Range
Set a = Range("C2: C" & Cells(i, 3).End(xlDown).Row)
Set b = Range("B2: B" & Cells(i, 3).End(xlDown).Row)
If Cells(i, 3).Value = "" Then
Cells(i, 3).Value = Application.WorksheetFunction.SumProduct(a, b)
End If
Next i
End Sub
我建议这个代码:
Option Explicit
Sub testAgain2GroupID()
Dim i As Long, im1 As Long, startRow As Long, lastPoint As Long
Dim a As Range, b As Range
startRow = 2
lastPoint = Cells(Rows.Count, 3).End(xlUp).Row
Do While (startRow < lastPoint)
'
' check id cell for group of identical id:
'
For i = startRow To lastPoint
If (Cells(i, 1).Value = "") Then
Exit For
End If
Next
'
' now we found the group:
'
im1 = i - 1
Set a = Range("C" & startRow & ":C" & im1)
Set b = Range("B" & startRow & ":B" & im1)
'
' so sumprod():
'
Cells(i, 3).Value = Application.WorksheetFunction.SumProduct(a, b)
'
' prepare for next loop:
'
startRow = i + 1
Loop
End Sub
我测试了它,它对我有用。
您可以 运行 重复此代码而不更改结果,因为 id 列中的空单元格永远不会被填充。
我想做一个循环,对用 Id 标识的每个组执行 SumProduct 操作。我的意思是我需要在第 2 列和第 3 列之间进行 SumProduct 操作,并且结果出现在该列的空单元格中。我已经尝试使用下一个代码,但它没有像我期望的那样工作。我曾尝试更改范围内的 Starpoint,但我的解决方案没有奏效。感谢您给我的反馈和帮助。
Startrow = 2
Lastpoint = Cells(Rows.Count, 3).End(xlUp).Row
For i = Startrow To Lastpoint + 1
If Cells(i, 3).Value = "" Then Cells(i, 3).Value = Application.WorksheetFunction.SumProduct(a, b)
a = Range("C2: C" & Cells(i, 3).End(xlDown).Row)
b = Range("B2: B" & Cells(i, 3).End(xlDown).Row)
Next i
您需要在代码中实际定义范围 a,b
。试试这个。
Option Explicit 'pay attention and read up on this. crucial to developing bug free code.
Sub test()
Dim startRow as Long, lastPoint as Long 'always, always declare variables and the type
startRow = 2
lastpoint = Cells(Rows.Count, 3).End(xlUp).Row
For i = Startrow To Lastpoint + 1
Dim a as Range, b as Range
Set a = Range("C2: C" & Cells(i, 3).End(xlDown).Row)
Set b = Range("B2: B" & Cells(i, 3).End(xlDown).Row)
If Cells(i, 3).Value = "" Then
Cells(i, 3).Value = Application.WorksheetFunction.SumProduct(a, b)
End If
Next i
End Sub
我建议这个代码:
Option Explicit
Sub testAgain2GroupID()
Dim i As Long, im1 As Long, startRow As Long, lastPoint As Long
Dim a As Range, b As Range
startRow = 2
lastPoint = Cells(Rows.Count, 3).End(xlUp).Row
Do While (startRow < lastPoint)
'
' check id cell for group of identical id:
'
For i = startRow To lastPoint
If (Cells(i, 1).Value = "") Then
Exit For
End If
Next
'
' now we found the group:
'
im1 = i - 1
Set a = Range("C" & startRow & ":C" & im1)
Set b = Range("B" & startRow & ":B" & im1)
'
' so sumprod():
'
Cells(i, 3).Value = Application.WorksheetFunction.SumProduct(a, b)
'
' prepare for next loop:
'
startRow = i + 1
Loop
End Sub
我测试了它,它对我有用。
您可以 运行 重复此代码而不更改结果,因为 id 列中的空单元格永远不会被填充。