使用VBA从选择中选择重复的最大数字

时间:2018-06-08 09:47:17

标签: excel vba excel-vba

这是我的代码:

Sub Largest()

Dim strData As String
Dim rng As Range
Dim vValue As Variant
Dim rngCol As Range
Dim lngRow As Long

Dim rngAdd As Range

'Find desired range in which to find the largest value
Dim selectionRange As Variant

selectionRange = Selection.Address(ReferenceStyle:=xlA1, _
                        RowAbsolute:=False, ColumnAbsolute:=False)
Set rng = Range(selectionRange)

'Determines largest value in range
vValue = Application.WorksheetFunction.Max(rng)

For Each rngCol In rng.Columns
    'Determines in case the largest value exists in a particular column
    If Application.WorksheetFunction.CountIf(rngCol, vValue) > 0 Then
        'Returns row number of the largest value, in the column which has the same
        lngRow = Application.WorksheetFunction.Match(vValue, rngCol, 0)

        'Returns cell address of the largest value
        Set rngAdd = rngCol.Cells(lngRow, 1)

        Dim cel As Range
        For Each cel In rng
            If (cel.Value = vValue) Then
                cel.Select
            End If
        Next cel
        Exit Sub
    End If
Next
End Sub

这是我的选择:

enter image description here

有两个最大的数字(重复)。该代码循环通过最大值,即111,但仅选择最大数字的最后一个循环单元,即D8

如何选择两个细胞?我可以用颜色突出显示单元格,因为它在重复时循环遍历所有最大数字。但我不想用颜色突出它们。我只想选择它们,例如按ctrl按钮并选择C6D8。 有什么帮助吗?

3 个答案:

答案 0 :(得分:4)

Option Explicit

Public Sub TestMe()

    Dim inputRange As Range
    Set inputRange = Selection

    Dim largestNumber As Double
    largestNumber = WorksheetFunction.Max(inputRange)

    Dim myUnion As Range
    Dim myCell As Range

    For Each myCell In inputRange
        If myCell = largestNumber Then
            If Not myUnion Is Nothing Then
                Set myUnion = Union(myUnion, myCell)
            Else
                Set myUnion = myCell
            End If
        End If
    Next myCell

    myUnion.Select

End Sub

Union()制作一系列单元格。因此,您只需选择通过Union()创建的变量。使用Union()的棘手部分就是你添加它的方式 - 你有两种情况:

If Not myUnion Is Nothing Then
    Set myUnion = Union(myUnion, myCell)
Else
    Set myUnion = myCell
End If

Application.Union MSDN

答案 1 :(得分:3)

@Vitaya的积分。我把他的anwer变成了一个.findnext sub

Option Explicit

Dim inputRange As Range
Dim c As Range
Dim firstAddress As String
Dim myUnion As Range
Dim myCell As Range
Dim largestNumber As Long

Public Sub TestMe1()

    Set inputRange = Selection
    largestNumber = WorksheetFunction.Max(inputRange)

    With inputRange
        Set c = .Find(largestNumber, LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                If Not myUnion Is Nothing Then
                    Set myUnion = Union(myUnion, c)
                Else
                    Set myUnion = c
                End If
                Set c = .FindNext(c)
            Loop While c.Address <> firstAddress
        End If
    End With

    myUnion.Select

End Sub

答案 2 :(得分:1)

关于这个问题,有人可能会读到这个 ​​- 就像按下ctrl按钮并选择C6和D8。

这非常有用,因为Excel拥有精彩的&#34; Macro-Recorder&#34; (这里没有讽刺意味)。因此,宏录制在这里确实是一个好主意:

它产生Range("C6,D8").Select

现在,如果某种方式C6,D8可以传递到某个范围并且选择了范围,那么该作业已经完成。因此,在每次迭代时,我们添加单元格的地址和之后的逗号。唯一的问题似乎是逗号,因为C6,D8,无效。这是通过一些切片来实现的:

toBeSelected = Left(toBeSelected, Len(toBeSelected) - 1)

这是整个代码:

Option Explicit

Public Sub TestMe()

    Dim inputRange As Range
    Set inputRange = Selection

    Dim largestNumber As Double
    largestNumber = WorksheetFunction.Max(inputRange)

    Dim toBeSelected As String
    Dim myCell As Range

    For Each myCell In inputRange
        If myCell = largestNumber Then
            toBeSelected = toBeSelected & myCell.Address & ","
        End If
    Next myCell

    toBeSelected = Left(toBeSelected, Len(toBeSelected) - 1)
    Range(toBeSelected).Select

End Sub