INDEX / MATCH MAX vba

时间:2019-01-22 11:08:05

标签: excel vba excel-formula max match

我的问题是要从范围(D2:D296)中查找包含范围(N2:N296)中最高值的学生姓名。我已经完成了以下代码,无法很好地执行。 不好,我之前没有提到我的数据来自diff工作表Sheet1。

sub maximummark()

Dim Result As Long
Dim rng As range
Dim rnng As range


Set rng = ThisWorkbook.Worksheets("Sheet1").range("$N$2:$N$296")
Set rnng= This Workbook.Worksheets("Sheet1").range("$D$2:$D$296")
Result = Index(rnng, Match(Max(rng), rng))

[B3] = Result

结束子

我对vba还是很陌生,我很感谢以前的评论,我的缺点是我刚才没有把它写得更清楚,而我的缺点是我写了怪异的代码。

3 个答案:

答案 0 :(得分:2)

好吧,看不到你做了什么,但是根据你的问题:

=INDEX(D2:D296,MATCH(MAX(N2:N296),N2:N296,0))

答案 1 :(得分:0)

您还可以使用:

    Option Explicit

    Sub HighestValue()

        Dim Lastrow As Long, MaxValue As Long
        Dim rngValues As Range, rngNames As Range
        Dim MaxName As String

        With ThisWorkbook.Worksheets("Sheet1")

            Lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row

            Set rngValues = .Range(Cells(2, 2), Cells(Lastrow, 2))
            Set rngNames = .Range(Cells(2, 1), Cells(Lastrow, 1))

            MaxValue = Application.Max(rngValues)

            MaxName = Application.WorksheetFunction.Index(rngNames, Application.WorksheetFunction.Match(MaxValue, rngValues, 0))

        End With

    End Sub

答案 2 :(得分:0)

如果要在VBA代码中使用本机工作表功能,则添加Application或WorksheetFunction。

<div class="theme-2">
    <div class="theme-1">
      <span class="primary">nok</span>
      <span class="secondary">nok</span>
      <span class="accent">nok</span>
      <span class="warn">nok</span>
    </div>
    <div class="theme-2">
      <span class="primary">ok</span>
      <span class="secondary">ok</span>
      <span class="accent">ok</span>
      <span class="warn">ok</span>
    </div>
    <div class="theme-3">
      <span class="primary">ok</span>
      <span class="secondary">ok</span>
      <span class="accent">ok</span>
      <span class="warn">ok</span>
    </div>
  </div>