MercuryVBA
Board Regular
- Joined
- Nov 12, 2013
- Messages
- 56
Hello, let's say I have a report with one sheet with the following column headers:
Sheet 1
<tbody>
</tbody>
So the report above is auto generated, and sometimes, the column headers may get shuffled around - for example the next time the report comes out Column C may have the Empl ID instead of column B.
This make is difficult for me to write any lookup code with a static Column reference to a range ... to solve the problem here is what I've come up with and need some help working out the syntax.
My KEY OBJECTIVE is to programatically find the starting cell address where the Empl ID header is located then dynamically select the range to the last row of the report. The report will always have a changing number of rows.
Thank you sincerely for your consideration and help!
Sheet 1
A | B | C | D | E | F | |
1 | Empl Name | Empl ID | Job Desc | Hrs | Etc2. | Etc3. |
2 | Jack | 1234 | PM | 3 | xxx | xxx |
3 | Jill | 2345 | Analyst | 6 | xxx | xxx |
4 | Tom | 3456 | Sales | 10 | xxx | xxx |
5 | Harry | 4567 | Engineer | 50 | xxx | xxx |
<tbody>
</tbody>
So the report above is auto generated, and sometimes, the column headers may get shuffled around - for example the next time the report comes out Column C may have the Empl ID instead of column B.
This make is difficult for me to write any lookup code with a static Column reference to a range ... to solve the problem here is what I've come up with and need some help working out the syntax.
My KEY OBJECTIVE is to programatically find the starting cell address where the Empl ID header is located then dynamically select the range to the last row of the report. The report will always have a changing number of rows.
Code:
Sub MacroTest()
Dim Report As Worksheet, StrtCell As Range
Set Report = Worksheets("Sheet 1")
FinalRowReport = Report.Cells(Rows.Count, 1).End(xlUp).Row
Set StrtCell = Reports.UsedRange.Find("Empl ID", lookat:=xlpart).Address
StrtCol = Split(StrtCell, "$")(0) 'Trying to get the column letter here
Report.Range(StrtCell & ":" & StrtCol & FinalRowWSSubALD).Select
'In the case of the example table I posted above, I would like the to select Range(B1:B5) with the above lines of code
End Sub
Thank you sincerely for your consideration and help!