【问题标题】:Get range top values and display in other sheet vba获取范围顶部值并在其他工作表 vba 中显示
【发布时间】:2016-10-07 10:39:20
【问题描述】:

我有一个工作簿,其中有一列是供应商名称,两列是总销售额。 在另一张表中,我想在一个列(单元格)中选择并显示前五名最佳销售额(数字),在下一列(单元格)中显示供应商的名字和姓氏。

我正在尝试解决这个问题,但我不太了解 vba。 我试过这个,但只得到了价值。

Sub best()
Dim FirstHt As String
Dim SecondHt As String
Dim ThirdHt As String
Dim FourthHt As String
Dim FifthHt As String

FirstHt = WorksheetFunction.Large(Sheets("Resumo").Range("J11:J47"), 1)
          Sheets("os melhores").Range("F30") = FirstHt
SecondHt = WorksheetFunction.Large(Sheets("Resumo").Range("J11:J47"), 2)
          Sheets("os melhores").Range("F31") = SecondHt
ThirdHt = WorksheetFunction.Large(Sheets("Resumo").Range("J11:J47"), 3)
          Sheets("os melhores").Range("F32") = ThirdHt
FourthHt = WorksheetFunction.Large(Sheets("Resumo").Range("J11:J47"), 4)
          Sheets("os melhores").Range("F33") = FourthHt
FifthHt = WorksheetFunction.Large(Sheets("Resumo").Range("J11:J47"), 5)
          Sheets("os melhores").Range("F34") = FifthHt
End Sub 

提前致谢

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    这将对您的第一页(名称 A,值 B)进行排序,取 5 个最高值,并将它们打印在工作表上,从大到小。

    Sub Testsort()
    Dim LastRow As Long, one As Long, two As Long, three As Long, four As Long, five As Long, onename As String, twoname As String, threename As String, fourname As String, fivename As String
    
    
    Range("B1").CurrentRegion.Select
    Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    
    LastRow = Range("A" & Rows.Count).End(xlUp).row
    
    
    Worksheets("Sheet1").Activate
    one = Cells((LastRow), "B").Value
    onename = Cells((LastRow), "A")
    two = Cells((LastRow - 1), "B").Value
    twoname = Cells((LastRow - 1), "A")
    three = Cells((LastRow - 2), "B").Value
    threename = Cells((LastRow - 2), "A")
    four = Cells((LastRow - 3), "B").Value
    fourname = Cells((LastRow - 3), "A")
    five = Cells((LastRow - 4), "B").Value
    fivename = Cells((LastRow - 4), "A")
    
    
    Worksheets("Sheet2").Activate
    Cells(1, "A") = onename
    Cells(1, "B") = one
    Cells(2, "A") = twoname
    Cells(2, "B") = two
    Cells(3, "A") = threename
    Cells(3, "B") = three
    Cells(4, "A") = fourname
    Cells(4, "B") = four
    Cells(5, "A") = fivename
    Cells(5, "B") = five
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      实际上不需要 VBA。

      =LARGE(array,k) 函数可用于返回范围内的最大、第二大、第三大和第 k 大值。

      我们也可以使用=MATCH (lookup_value, lookup_array, [match_type])函数和INDEX(array, row_num, [column_num])函数来获取变量名。

      请参阅http://www.excel-user.com/2011/02/large-functionget-top-n-values-from.html 以获取更多指南。

      【讨论】:

      • 嗨乔希,我试过了,但我无法弄清楚重复项,它传递了所有的名字,而不仅仅是第一个和最后一个。我更喜欢vba方法。无论如何,谢谢。
      【解决方案3】:

      这也将处理重复。

      我的代码从下面的列中获取值。如果您有任何更改,您可以更改代码。

      表 1(已获取数据)- J 有销售额,G 有供应商详细信息

      Sheet2(数据已复制)- F 销售额,G 供应商名字,H 供应商姓氏详细信息

      Sub best()
      Dim maxvalue As Long
      Dim copyrow As Long
      copyrow = 30
      Dim prevval As Long
      Dim prevrow As Long
      Dim i As Long
      Dim fndrow As Long
      
      prevval = 0
      prevrow = 0
      
      For i = 1 To 5
      maxvalue = WorksheetFunction.Large(Sheets("Resumo").Range("J11:J47"), i)
      
      If maxvalue <> prevval Then
      fndrow = Sheets("Resumo").Range("J11:J47").Find(What:=maxvalue, LookIn:=xlValues, lookat:=xlWhole).Row
      Else
      fndrow = Sheets("Resumo").Range("J" & prevrow & ":J47").Find(What:=maxvalue, LookIn:=xlValues, lookat:=xlWhole).Row
      End If
      
      Dim vendor As String
      vendor = Sheets("Resumo").Range("G" & CStr(fndrow))
      
      Sheets("os melhores").Range("F" & CStr(copyrow)) = maxvalue
      If InStr(vendor, " ") <> 0 Then
      Sheets("os melhores").Range("G" & CStr(copyrow)) = Left(vendor, InStr(vendor, " "))
      Sheets("os melhores").Range("H" & CStr(copyrow)) = Right(vendor, InStr(vendor, " "))
      Else
      Sheets("os melhores").Range("G" & CStr(copyrow)) = Sheets("Resumo").Range("G" & CStr(fndrow))
      End If
      
      prevval = maxvalue
      prevrow = fndrow
      copyrow = copyrow + 1
      
      Next i
      
          End Sub
      

      【讨论】:

      • 给了一些未声明的变量
      • 将工作表名称更改为 Resumo 和 os melhores。请立即检查。
      • 我已经这样做了,并且确实添加了:Dim i As Long 和 Dim fndrow As Long。它的工作就像一个魅力!只有一件事。供应商名称是全名,我只需要传递第一个和最后一个。你能帮忙吗?提前致谢!
      • 它们是否用空格分隔。任何分隔符?
      • 所有名字都用空格隔开,比如“Mary Jonas de Oliveira”,不带括号。
      【解决方案4】:

      在 KannanRG 的宝贵帮助下解决了。 我改变了这一行的右边:

      Sheets("os melhores").Range("H" & CStr(copyrow)) = Right(vendor, Len(vendor) - InStrRev(vendor, " "))
      

      正确的值等于全名的长度减去姓的长度。

      感谢所有阅读并帮助我解决此问题的人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        相关资源
        最近更新 更多