*VLOOKUP to create list of rows on each worksheet*

*VLOOKUP to create list of rows on each worksheet*

December 8, 2017

The Tip of the Night for December 13, 2016 concerned VBA code that you can use to generate a list of the ranges of cells with data on all worksheets in a workbooks.Â Â Once you have the ranges generated on a worksheet you may then want to create sets of formulas on a master worksheet that enters all of the date from the original worksheet.Â

Â

You can go about this in the following manner.Â

Â

Parse out the ranges (using the Text to Columns tool)Â Â for the data on each worksheet so you haveÂ the last row on each worksheet in a column. (In this example see column H).Â Â In the column to the right use a simpleÂ addition formula to add the number of rows from the preceding worksheet to those from the next worksheet, so that we end up with the grand total number of rows on each worksheet.Â (In this example see cell I5).Â Â Then put the names of each worksheet in the column to the right.Â (in this example see column J).Â Â Â

Â

Â

In column K generate a list of numbers from 1 to whatever the total number of rows on all worksheets is.Â Â In this example we have from 1 to 280204.Â Â

Â

Now we're going to use a modified form of the standard VLOOKUP formula to generate Â cell entries containing the name of each worksheet so that each entry corresponds to the number of rows on the worksheet.Â Â To this enter the VLOOKUP formula like this:

Â

=VLOOKUP(K2,I:J,2)

Â

So when the row number looked up in column K reaches the row number that the next worksheet begins on in the total count for all rows it generates a reference to that worksheet name.Â Â The key is to omit the FALSE or TRUE reference usuallyÂ included at the end of a VLOOKUP formula.

Â

The goal here is to generate a list of HLOOKUP formulas in multiple cells on the master worksheet which will pull the data from each successive worksheet.Â Â

Â

Generate in column M numbers keeping track of the row count for each worksheet.Â Just use a formula like this:

Â

=IF(L3<>L2,1,M2+1)

Â

EnterÂ a 1 in the top cell, this formula in the next cell and then pull it down.Â Â

Â

Now we're ready to edit the resulting data so that we can generate HLOOKUP formulas which will search across the the column headings in row A of each worksheet and pull the resulting data to the master worksheet.Â So we enter text like this in NotePad ++:

Â

Sheet3Â Â Â 1

PitchingÂ Â Â 1

PitchingÂ Â Â 2

PitchingÂ Â Â 3

PitchingÂ Â Â 4

PitchingÂ Â Â 5

PitchingÂ Â Â 6

PitchingÂ Â Â 7

PitchingÂ Â Â 8

PitchingÂ Â Â 9

PitchingÂ Â Â 10

PitchingÂ Â Â 11

PitchingÂ Â Â 12

Â

Â

. . . and then edit it so we end up with:

Â

=HLOOKUP(A$1,Sheet3!$1:$17100,ROW(A1),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A1),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A2),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A3),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A4),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A5),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A6),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A7),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A8),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A9),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A10),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A11),FALSE)

=HLOOKUP(A$1,Pitching!$1:$17100,ROW(A12),FALSE)

Â

The end of theÂ range should correspond to the highest row number with data on any of the worksheet, in this example 171000.Â Â Enter the column headings from one worksheet in row A on the master worksheet.Â Â Then enter in the formula created in NotePad ++, or another text editor, in column A, and pull over the formulas with CTRL + R, so the data isÂ filled in each column with a matching heading.Â Â Â

Â

Â

Â

Â

Â

Â