【发布时间】:2023-02-02 22:10:35
【问题描述】:
以下场景: 我有不同的地区和不同的产品组。通过单元格 A1 中的下拉菜单区域和产品组通过单元格 A2 中的下拉菜单。在单元格 C3 中,我有一个公式取决于 A1 和 A2 的选择。 现在我想遍历不同的区域并为所有不同区域的每个产品组获取 C3 的最大值。另一个问题是有时 C3 会导致错误,因为 A1 和 A2 中的组合没有结果......
那是我的尝试,但不幸的是我的技能已达到极限。如果您能提供帮助,将不胜感激。谢谢
Sub FindMax()
Dim maxValue As Variant
Dim currentValue As Variant
Dim i As Integer
Dim j As Integer
Dim regions As Variant
Dim productGroups As Variant
regions = Array("Region 1", "Region 2", "Region 3")
productGroups = Array(1, 2, 3, 4, 5)
For i = LBound(regions) To UBound(regions)
Range("A1").Value = regions(i)
For j = LBound(productGroups) To UBound(productGroups)
Range("A2").Value = productGroups(j)
currentValue = Range("C3").Value
If j = LBound(productGroups) Then
maxValue = currentValue
ElseIf currentValue > maxValue Then
maxValue = currentValue
End If
Next j
Next i
MsgBox "The highest value for product group " & ws1.Range("A2").Value & " across all regions is: " & maxValue
End Sub
【问题讨论】: