Vba 找到最大值

Vba 找到最大值,vba,excel,Vba,Excel,假设我必须找到重复滚动的最大值,比如10的输出将是900(最大值为900700800800) 我能找到副本,但找不到最大值 roll marks 10 900 10 700 10 800 20 400 20 400 30 1700 40 1800 10 800 子sbFindDuplicatesInColumn() 最后一排一样长 与索引一样长 暗iCntr与长iCntr相同 lastRow=范围(“H65000”)。结束(xlUp)。行 对于iCntr=5至最后一行 Dim

假设我必须找到重复滚动的最大值,比如10的输出将是900(最大值为900700800800)

我能找到副本,但找不到最大值

roll marks
10  900
10  700
10  800
20  400
20  400
30  1700
40  1800
10  800
子sbFindDuplicatesInColumn()
最后一排一样长
与索引一样长
暗iCntr与长iCntr相同
lastRow=范围(“H65000”)。结束(xlUp)。行
对于iCntr=5至最后一行
Dim intArr(1000)为整数
暗色计数器
i计数器=0
如果单元格(iCntr,8)“,则
matchFoundIndex=WorksheetFunction.Match(单元格(iCntr,8),范围(“H1:H”和lastRow),0)
如果iCntr匹配FoundIndex,则
单元格(iCntr,10)=“重复”
如果结束
如果结束
下一个
端接头

我会这样尝试,使用字典作为索引并循环。它不如数组快,因此根据您的数据大小,它可能会慢一些。您可以做任何事情,而不是
msgbox
-

Sub sbFindDuplicatesInColumn()
    Dim lastRow As Long
    Dim matchFoundIndex As Long
    Dim iCntr As Long
    lastRow = Range("H65000").End(xlUp).Row

    For iCntr = 5 To lastRow
    Dim intArr(1000) As Integer
    Dim iCounter
    iCounter = 0
    If Cells(iCntr, 8) <> "" Then
        matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 8), Range("H1:H" & lastRow), 0)
        If iCntr <> matchFoundIndex Then

            Cells(iCntr, 10) = "Duplicate"
       End If
    End If
    Next
End Sub

我会这样尝试,使用字典作为索引并循环使用。它不如数组快,因此根据您的数据大小,它可能会慢一些。您可以做任何事情,而不是
msgbox
-

Sub sbFindDuplicatesInColumn()
    Dim lastRow As Long
    Dim matchFoundIndex As Long
    Dim iCntr As Long
    lastRow = Range("H65000").End(xlUp).Row

    For iCntr = 5 To lastRow
    Dim intArr(1000) As Integer
    Dim iCounter
    iCounter = 0
    If Cells(iCntr, 8) <> "" Then
        matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 8), Range("H1:H" & lastRow), 0)
        If iCntr <> matchFoundIndex Then

            Cells(iCntr, 10) = "Duplicate"
       End If
    End If
    Next
End Sub

使用colsAB格式的数据使用:

Sub test()
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")
Dim lastrow As Long
lastrow = Range("H65000").End(xlUp).Row
Dim icntr As Long

For icntr = 5 To lastrow
Dim val As Long
val = Cells(icntr, 8)
dict(val) = 1
Next

Dim maxval As Long
For Each Key In dict.keys
    maxval = 1
    For icntr = 5 To lastrow

        If Cells(icntr, 8) = Key Then
            If Cells(icntr, 9) > maxval Then
                maxval = Cells(icntr, 9)
            End If
        End If
    Next
    MsgBox ("maximum for " & Key & " is " & maxval)

Next

End Sub


这是因为VBA将采用数组公式。

数据为colsAB使用:

Sub test()
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")
Dim lastrow As Long
lastrow = Range("H65000").End(xlUp).Row
Dim icntr As Long

For icntr = 5 To lastrow
Dim val As Long
val = Cells(icntr, 8)
dict(val) = 1
Next

Dim maxval As Long
For Each Key In dict.keys
    maxval = 1
    For icntr = 5 To lastrow

        If Cells(icntr, 8) = Key Then
            If Cells(icntr, 9) > maxval Then
                maxval = Cells(icntr, 9)
            End If
        End If
    Next
    MsgBox ("maximum for " & Key & " is " & maxval)

Next

End Sub


这是因为VBA将采用数组公式。

您可以使用自动筛选查找重复项,然后使用小计函数查找最大值

Sub dural()
   MsgBox Evaluate("MAX(IF(A2:A9=10,B2:B9))")
End Sub

您可以使用自动筛选查找重复项,然后使用小计函数查找最大值

Sub dural()
   MsgBox Evaluate("MAX(IF(A2:A9=10,B2:B9))")
End Sub

您是否使用
intar
执行任何操作?我将数字加载到一个数组中,然后从中找到最大值。实际上,我试图在某个数组中填充数据并从中找到最大值,所以我使用了intArr。你用intArr做什么?我将这些数字加载到一个数组中,从那里找到最大值。实际上,我试图填充一些数组中的数据并从中找到max,所以我使用的是InTyryeAh,如果它是那样的硬编码,那我就不需要考虑了。他也可以只使用纸上的公式来参考他想要的那一卷。或者只是使用一个枢轴表。@ RayStFaLaRi我认为你的方法比我的灵活得多。是的,如果它是那样的硬编码,那我就不需要考虑了。他也可以只使用纸上的公式来参考他想要的那一卷。或者只使用数据透视表。@Raystafarian我认为你的方法比我的灵活得多。