excel 将单元格的值与下一轮循环中的相同单元格进行比较

wkyowqbh  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(200)

以下场景:我有不同的区域和不同的产品组。单元格A1中的Region via DropDown和单元格A2中的Product Group via DropDown。在单元格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
insrf1ej

insrf1ej1#

查找两个下拉菜单的所有组合的最大值

  • 如果你确信至少有一个数值,就不需要最后的if语句;如果你确信至少有一个正值,就不需要IsFirstFoundIsMax,但为什么要冒这个险呢?
Option Explicit

Sub MaxAcrossRegions()
    
    Dim Regions(): Regions = Array("Region 1", "Region 2", "Region 3")
    Dim ProductGroups(): ProductGroups = Array(1, 2, 3, 4, 5)
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    Dim RegionCell As Range: Set RegionCell = ws.Range("A1")
    Dim ProductGroupCell As Range: Set ProductGroupCell = ws.Range("A2")
    Dim ValueCell As Range: Set ValueCell = ws.Range("C3")
    
    Dim CurrValue As Variant, MaxValue As Double, r As Long, p As Long
    Dim MaxProductGroup As String, MaxRegion As String
    Dim IsFirstFound As Boolean, IsMax As Boolean
    
    For r = LBound(Regions) To UBound(Regions)
        RegionCell.Value = Regions(r)
        For p = LBound(ProductGroups) To UBound(ProductGroups)
            ProductGroupCell.Value = ProductGroups(p)
            CurrValue = ValueCell.Value
            If IsNumeric(CurrValue) Then
                If IsFirstFound Then
                    If CurrValue > MaxValue Then IsMax = True
                Else
                    IsFirstFound = True ' set once, on the first numeric value
                    IsMax = True
                End If
                If IsMax Then
                    MaxValue = CurrValue
                    MaxProductGroup = ProductGroups(p)
                    MaxRegion = Regions(r)
                    IsMax = False ' reset for next iteration
                End If
            End If
        Next p
    Next r
    
    If IsFirstFound Then
        MsgBox "The highest value for a product group across all regions is " _
            & MaxValue & ", found in product group " & MaxProductGroup _
            & " of region " & MaxRegion & ".", vbInformation
    Else
        MsgBox "No numeric values found.", vbCritical
    End If
    
End Sub

每个产品组

  • 外循环遍历产品组,内循环遍历区域。每个内循环完成后,显示当前产品组的结果。
Sub MaxPerProductGroup()
    
    Dim Regions(): Regions = Array("Region 1", "Region 2", "Region 3")
    Dim ProductGroups(): ProductGroups = Array(1, 2, 3, 4, 5)
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    Dim RegionCell As Range: Set RegionCell = ws.Range("A1")
    Dim ProductGroupCell As Range: Set ProductGroupCell = ws.Range("A2")
    Dim ValueCell As Range: Set ValueCell = ws.Range("C3")
    
    Dim CurrValue As Variant, MaxValue As Double, r As Long, p As Long
    Dim MaxRegion As String, IsFirstFound As Boolean, IsMax As Boolean
    
    For p = LBound(ProductGroups) To UBound(ProductGroups)
        ProductGroupCell.Value = ProductGroups(p)
        For r = LBound(Regions) To UBound(Regions)
            RegionCell.Value = Regions(r)
            CurrValue = ValueCell.Value
            If IsNumeric(CurrValue) Then
                If IsFirstFound Then
                    If CurrValue > MaxValue Then IsMax = True
                Else
                    IsFirstFound = True
                    IsMax = True
                End If
                If IsMax Then
                    MaxValue = CurrValue
                    MaxRegion = Regions(r)
                    IsMax = False ' reset for next iteration
                End If
            End If
        Next r
        If IsFirstFound Then
            MsgBox "The highest value for product group " & ProductGroups(p) _
                & " across all regions is " & MaxValue _
                & ", found in region " & MaxRegion & ".", _
                vbInformation
            IsFirstFound = False ' reset for next iteration
        Else
            MsgBox "No numeric values found in product group " _
                & ProductGroups(p) & ".", vbCritical
        End If
    Next p
    
End Sub

每个区域

  • 外部循环循环通过区域,内部循环通过产品组。每个内部循环完成后,将显示当前区域的结果。
Sub MaxPerRegion()
    
    Dim Regions(): Regions = Array("Region 1", "Region 2", "Region 3")
    Dim ProductGroups(): ProductGroups = Array(1, 2, 3, 4, 5)
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    Dim RegionCell As Range: Set RegionCell = ws.Range("A1")
    Dim ProductGroupCell As Range: Set ProductGroupCell = ws.Range("A2")
    Dim ValueCell As Range: Set ValueCell = ws.Range("C3")
    
    Dim CurrValue As Variant, MaxValue As Double, r As Long, p As Long
    Dim MaxProductGroup As String, IsFirstFound As Boolean, IsMax As Boolean
    
    For r = LBound(Regions) To UBound(Regions)
        RegionCell.Value = Regions(r)
        For p = LBound(ProductGroups) To UBound(ProductGroups)
            ProductGroupCell.Value = ProductGroups(p)
            CurrValue = ValueCell.Value
            If IsNumeric(CurrValue) Then
                If IsFirstFound Then
                    If CurrValue > MaxValue Then IsMax = True
                Else
                    IsFirstFound = True
                    IsMax = True
                End If
                If IsMax Then
                    MaxValue = CurrValue
                    MaxProductGroup = ProductGroups(p)
                    IsMax = False ' reset for next iteration
                End If
            End If
        Next p
        If IsFirstFound Then
            MsgBox "The highest value for region " & Regions(r) _
                & " across all product groups is " & MaxValue _
                & ", found in product group " & MaxProductGroup & ".", _
                vbInformation
            IsFirstFound = False ' reset for the next iteration
        Else
            MsgBox "No numeric values found in region " _
                & Regions(r) & ".", vbCritical
        End If
    Next r
    
End Sub

相关问题