Sumproduct VBA而不是公式

下面的问题已经困扰了我很长一段时间了。

我使用以下公式将年收入(范围A2:A10)中的收入(范围B2:B10)进行sorting:

=SUMPRODUCT(($A$2:$A$10=A10)*($B$2:$B$10>B10))+1

我在范围(C2:C10)中粘贴这个公式来找出每个观察的等级。

这一切工作正常,但是,这里有这个问题:我不需要这10个观测值,而是124440个观测值; 运行这个瘫痪我的工作簿过多的时间。

所以我的问题是:你能帮助我find一种方法,它可以让我计算上述公式,而不必复制/粘贴到每个单元格(可能通过使用VBA),这反过来将允许计算运行多更快?

我已经search了网页,但是,因为我是一个VBA新手,我不知道我应该寻找什么

 Company Name Year - Fiscal Revenues Rank AAR CORP 2003 150 1 AAR CORP 2004 180 1 AAR CORP 2005 120 2 ADC TELECOMMUNICATIONS INC 2003 90 3 ADC TELECOMMUNICATIONS INC 2004 99 3 ADC TELECOMMUNICATIONS INC 2005 108 3 AFP IMAGING CORP 2003 120 2 AFP IMAGING CORP 2004 130 2 AFP IMAGING CORP 2005 140 1 

按您的要求。 而不是10个观察认为124440。

这不是一个VBA解决scheme,而是一个替代scheme。

ColumnA作为CompanyName ,ColumnB作为Year ,ColumnC作为Revenue ,ColumnD作为Rank

首先,对数据进行sorting

  1. (B栏)
  2. 然后由收入(C列)

在等级栏上,

  1. 对于第一行数据,键入1作为等级。
  2. 对于第二行数据,在此公式中键入: =IF(B3=B2,D2 + 1, 1)

公式是这样说的:

if the year is the same increase the rank number by 1 else reset the rank as 1

将公式向下拖动。

公式计算相当快我认为,但为了保留指定的排名,你需要复制和select性粘贴,只有值,用一个值replace公式。 这样你应该决定对整个桌子进行sorting,你不会放弃排名

不知道你们中有多less人仍然关注这个post。 但是为了充分披露,这正是我所期望的,而且要快得多(在2分钟而不是15分钟内完成了124000个obs的工作):

 Sub Rank() Dim LastRow As Long Dim LastCol As Long Application.ScreenUpdating = False ThisWorkbook.Sheets("Sheet2").Select LastRow = Cells(Rows.Count, "B").End(xlUp).Row With ActiveSheet.UsedRange LastCol = .Columns.Count + .Columns(1).Column - 1 End With 'Create a temporary column so that it can be used to re-sort data to its original state Cells(2, LastCol + 1).Value = 1 Cells(3, LastCol + 1).Value = 2 Cells(2, LastCol + 1).Resize(2).AutoFill Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)) 'Sort the data by Column B, in ascending order Range("A1", Cells(LastRow, LastCol + 1)).Sort key1:=Range("B1"), order1:=xlAscending, _ Header:=xlYes, ordercustom:=1, MatchCase:=False, Orientation:=xlTopToBottom 'Select B2 prior to creating a dynamic named range, since it uses relative references Range("B2").Select 'Create the dynamic named range ThisWorkbook.Names.Add "MyDynRng", "=INDEX(R2C2:R" & LastRow & "C2,MATCH(RC2,R2C2:R" & _ LastRow & "C2,0)):INDEX(R2C6:R" & LastRow & "C6,MATCH(RC2,R2C2:R" & LastRow & "C2,1))" 'Put the header for the ranking column Cells(1, LastCol + 2).Value = "Rank" 'Enter the ranking formula and convert them to values With Range(Cells(2, LastCol + 2), Cells(LastRow, LastCol + 2)) .FormulaR1C1 = "=COUNTIFS(INDEX(MyDynRng,0,1),RC2,INDEX(MyDynRng,0,5),"">""&RC6)+1" .Value = .Value End With 'Re-sort the data to its original state Range("A1", Cells(LastRow, LastCol + 2)).Sort key1:=Cells(1, LastCol + 1), order1:=xlAscending, _ Header:=xlYes, ordercustom:=1, MatchCase:=False, Orientation:=xlTopToBottom 'Delete the defined name ThisWorkbook.Names("MyDynRng").Delete 'Delete the temporary column Cells(1, LastCol + 1).EntireColumn.Delete 'To change the Lookup Column, adjust column numbers in the dynamic range (For example C5 to C6 if you want to go from column E to F Application.ScreenUpdating = True End Sub 

这被一个叫Domenic的人给了我一个build议。 不知道他是否在Stackoverflow活跃,但如果他是,感谢哥们。