获取范围最高值并显示在其他工作表中

时间:2016-10-07 10:39:20

标签: excel vba excel-vba

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

我试图解决这个问题,但我不了解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 

提前致谢

4 个答案:

答案 0 :(得分:0)

实际上不需要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

答案 1 :(得分:0)

这将对您的第一页(名称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 :(得分:0)

这也将涉及重复。

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

表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

答案 3 :(得分:0)

在KannanRG的宝贵帮助下解决了它。 我更改了这一行的权利:

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

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

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