简体   繁体   English

Excel VBA-对动态范围使用查找公式

[英]Excel VBA - Using lookup formula for a dynamic range

I am trying to use lookup function with VBA for a dynamic range. 我正在尝试将查询功能与VBA一起用于动态范围。 The figure below is an example of few lines of data that I am trying to test with. 下图是我尝试测试的几行数据的示例。 The top two rows are my header rows that represent range of 'heights' for each 'year' of available data. 前两行是我的标题行,它们代表可用数据“年”的“高度”范围。

The data in black font is my row data. 黑色字体的数据是我的行数据。 From where with code I am getting the red font data which are just the max values (ie the largest values) for each height for all available years for that height. 从哪里可以得到代码的红色字体数据,这些数据只是该高度在所有可用年份中每个高度的最大值(即最大值)。 Now I am also trying to find the year of the max value with the lookup function in VBA and paste to the right hand cells as shown in blue font in Figure 1. I can do this for a selected range using the following code and produce the output as shown in Figure 1 but I could not logically think of how to do this for a dynamic range. 现在,我还尝试使用VBA中的查找功能查找最大值的年份,然后粘贴到右侧单元格中,如图1中的蓝色字体所示。我可以使用以下代码对选定的范围执行此操作,并生成输出如图1所示,但我无法在逻辑上考虑如何针对动态范围执行此操作。

My problem is that my row data that is highlighted in black is dynamic range and data highlighted in red also will be of a dynamic range depending of number of 'heights'. 我的问题是,以黑色突出显示的行数据是动态范围,而以红色突出显示的数据也将是动态范围,具体取决于“高度”的数量。 So I am struggling to think of a logical way of setting the two ranges of black and red texts and find the year of the maximum value as shown in blue colour in figure 1. I would be greatful if someone could give me some advise on how I could approach to this problem. 因此,我在努力思考设置黑色和红色文本两个范围并找到最大值的年份的合理方法,如图1中的蓝色所示。如果有人可以给我一些建议,我将非常感激。我可以解决这个问题。 Thanks in advance! 提前致谢!

Sub Lookup()
         Range("K3").Select
        ActiveCell.FormulaR1C1 = _
        "=LOOKUP(RC[-3],RC[-10]:RC[-4],R[-1]C[-10]:R[-1]C[-4])"
        Range("K3").Select
    ActiveCell.FormulaR1C1 = "=LOOKUP(RC[-3],RC1:RC7,R2C1:R2C7)"
    Range("K3").Select
    Selection.AutoFill Destination:=Range("K3:M3"), Type:=xlFillDefault
    Range("K3:M3").Select
    Selection.AutoFill Destination:=Range("K3:M5"), Type:=xlFillDefault
    Range("K3:M5").Select
End Sub

在此处输入图片说明

This isn't exactly what you have going, but since I already worked it up and Tested it. 这并不完全是您要执行的操作,但是由于我已经完成并测试了它。 Working, it delivers the results you wanted and allows for more years to be input later. 工作时,它会提供您想要的结果,并允许以后输入更多年。

Private Sub FilterMax()

Dim max10 As Single
Dim max20 As Single
Dim max30 As Single

Dim max10Year As Long
Dim max20Year As Long
Dim max30Year As Long

Dim row As Long
Dim lastRow As Long

Dim firstYear As Long
Dim lastYear As Long
Dim year As Long

Dim sheet As String

lastRow = Sheets("MaxValues").Range("A" & Rows.Count).End(xlUp).row

'You might want to put an input box up or just manually set this.
firstYear = 2012
lastYear = 2014

For row = 2 To lastRow
'reset max for each DataRow
max10 = 0
max10Year = 0
max20 = 0
max20Year = 0
max30 = 0
max30Year = 0

    For year = firstYear To lastYear

    sheet = CStr(year)
    'Max10
    If Sheets(sheet).Cells(row, 2) > max10 Then
        max10 = Sheets(sheet).Cells(row, 2)
        max10Year = Sheets(sheet).Range("G1")
    End If
    'Max20
    If Sheets(sheet).Cells(row, 3) > max20 Then
        max20 = Sheets(sheet).Cells(row, 3)
        max20Year = Sheets(sheet).Range("G1")
    End If
    'Max30
    If Sheets(sheet).Cells(row, 4) > max30 Then
        max30 = Sheets(sheet).Cells(row, 4)
        max30Year = Sheets(sheet).Range("G1")
    End If

    Next year

    Sheets("MaxValues").Cells(row, 2).Value = max10
    Sheets("MaxValues").Cells(row, 2).Font.Color = vbRed

    Sheets("MaxValues").Cells(row, 3).Value = max10Year
    Sheets("MaxValues").Cells(row, 3).Font.Color = vbBlue

    Sheets("MaxValues").Cells(row, 4).Value = max20
    Sheets("MaxValues").Cells(row, 4).Font.Color = vbRed

    Sheets("MaxValues").Cells(row, 5).Value = max20Year
    Sheets("MaxValues").Cells(row, 5).Font.Color = vbBlue

    Sheets("MaxValues").Cells(row, 6).Value = max30
    Sheets("MaxValues").Cells(row, 6).Font.Color = vbRed

    Sheets("MaxValues").Cells(row, 7).Value = max30Year
    Sheets("MaxValues").Cells(row, 7).Font.Color = vbBlue

Next row

End Sub

MaxValues201220132014

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM