Simpler way to perform a row/column lookup in Excel, where columns with data are of variable lengths?

StackOverflow https://stackoverflow.com/questions/19503734

  •  01-07-2022
  •  | 
  •  

質問

I have Excel data that looks like this:

      CHEVY_12       FORD_12        FORD_13        CHEVY_13
t1    462.0646058    5.507611655    887.4193417    197.1054415
t2    427.4865042    646.2186952                   685.0746363
t3    175.4137935                                  73.77079157

The columns are all mixed up, and the column headers aren't in any meaningful order.

My goal is to put the data from this table into a table so that it looks like this:

<cell A1>  Chevy          Ford
2012       175.4137935     646.2186952
2013        73.77079157     887.4193417

However, in the data table, not all of the columns have data for t1, t2, and t3, so a straight HLOOKUP doesn't work. Basically the procedure I'm trying to use has four parts (all formulas assume we're talking about the Chevy 2012 cell, but see the attached screenshot for all the gory details):

  • Create the column header that's going to be looked up in the data table. Assuming the results table has its leftmost corner in cell A1 (as shown), I did this with =CONCATENATE(UPPER(B$1), "_", MID($A2, 3, 2)) in the first cell (Chevy, 2012), etc. as shown.

  • Find the column in the data table with a header that matches the header constructed in step 1. I just used =MATCH(B3, $F$6:$I$6, 0) for this.

  • Find the last row with data in each column, based on the column header. I used =MATCH(9E+307, OFFSET($E$6, 1, B9, MATCH(REPT("z", 255), $E$7:$E$9), 1)) for this. Since I don't know the maximum number of rows going in (in other words, how many t1, t2, t3, etc. I'm going to have), I had to find the last row with text in the row labels as well. I used formulas from this link to find the last number (MATCH(9E+307,range)) or text (MATCH(REPT("z",255),range)) in a range.

  • Use the pieces constructed above to perform the final HLOOKUP in the data table.

Here is the final spreadsheet:

spreadsheet

The data table is located in cells $E$6:$I$9 and looks like this:

data table

Is there a simpler way to perform a lookup like this? I combined all of these pieces into one formula, like this (the example formula looks up the data for Chevy, 2012):

=HLOOKUP(CONCATENATE(UPPER(B$2), "_", MID($A3, 3, 2)), $F$6:$I$9, MATCH(9E+307, OFFSET($E$6, 1, MATCH(CONCATENATE(UPPER(B$2), "_", MID($A3, 3, 2)), $F$6:$I$6, 0), MATCH(REPT("z", 255), $E$7:$E$9), 1)) + 1)

but obviously that's really messy, and I'm wondering if there is a simpler way. I'd like to do this with one formula (as in the last example), but that last formula does things I don't like, like performing the concatenation twice). I'm using Excel 2013.

役に立ちましたか?

解決

You could use a User Defined Function.

Something like

Function MyUDF(Brand As Variant, Yr As Variant, Data As Range) As Variant
    Dim Hdr As String
    Dim DataCol As Long

    Hdr = UCase(Brand) & "_" & Right$(Yr, 2)
    DataCol = Application.Match(Hdr, Data.Rows(1), 0)
    With Data.Columns(DataCol)
        If Len(.Cells(.Rows.Count, 1)) = 0 Then
            MyUDF = .Cells(.Rows.Count, 1).End(xlUp)
        Else
            MyUDF = .Cells(.Rows.Count, 1)
        End If
    End With
End Function

Called like this (based on your sample data addresses) and copied to the required range

=MyUDF(B$2,$A3,$E$6:$I$9)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top