Return Column Letter For Use in Dynamic Range Reference

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
ABCDEF
1Empl NameEmpl IDJob DescHrsEtc2.Etc3.
2Jack1234PM3xxxxxx
3Jill2345Analyst6xxxxxx
4Tom3456Sales10xxxxxx
5Harry4567Engineer50xxxxxx

<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!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
This make is difficult for me to write any lookup code with a static Column reference to a range

Non VBA solution.

With index / match it can be done.
 
Upvote 0
I don't have time to code it for you, but I'll give you an idea.
Create a Do Until Loop that starts at A1. Have the 1 be a variable and add 1 every time it goes through the loop.
In that loop, check if range("A"&variable).value equals "Emp ID"
IF it does, THEN output the column letter. END IF
Make another DO Until LoOP that finds the last cell in the first row that isn't blank and return it's column letter.
Have your output equal the output from your first Do Until Loop &":"& the output from the second do loop.
Put this into a function rather than a subroutine.
Then you can put it in your equation like this...
=VLOOKUP("1234",myFunction(),.....,FALSE)
Notice I put a bunch of dots. You didn't specify what the output column was. The output column will have to be dynamic, so your going to have to create another function that finds the column that you want to output based on the column header. Just as you did in the first function. Sounds hard, it's not. Just takes time. Then your formula will look like this.
=VLOOKUP("1234",myFunction(),secondFunction(),FALSE)
 
Upvote 0
Hello, let's say I have a report with one sheet with the following column headers:

Sheet 1
ABCDEF
1Empl NameEmpl IDJob DescHrsEtc2.Etc3.
2Jack1234PM3xxxxxx
3Jill2345Analyst6xxxxxx
4Tom3456Sales10xxxxxx
5Harry4567Engineer50xxxxxx

<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 & [B][COLOR=#ff0000]FinalRowWSSubALD[/COLOR][/B]).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!
Where is the variable "FinalRowWSSubALD" defined/set?
 
Upvote 0
Hello, let's say I have a report with one sheet with the following column headers:

Sheet 1
ABCDEF
1Empl NameEmpl IDJob DescHrsEtc2.Etc3.
2Jack1234PM3xxxxxx
3Jill2345Analyst6xxxxxx
4Tom3456Sales10xxxxxx
5Harry4567Engineer50xxxxxx

<tbody>
</tbody>

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
Does this do what you want...
Code:
Sub MacroTest()
  Dim Report As Worksheet, StrtCell As Range, FinalRowReport As Long
  Set Report = Worksheets("Sheet 1")
  
  FinalRowReport = Report.Cells(Rows.Count, 1).End(xlUp).Row
  Report.Rows(1).Find("Empl ID", LookAt:=xlWhole, MatchCase:=False).Resize(FinalRowReport).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
 
Upvote 0
Does this do what you want...
Code:
Sub MacroTest()
  Dim Report As Worksheet, StrtCell As Range, FinalRowReport As Long
  Set Report = Worksheets("Sheet 1")
  
  FinalRowReport = Report.Cells(Rows.Count, 1).End(xlUp).Row
  Report.Rows(1).Find("Empl ID", LookAt:=xlWhole, MatchCase:=False).Resize(FinalRowReport).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

Hello Rick - it does not because Empl ID may also be located on any row. Thank you for the suggestion.
 
Upvote 0
Hello Rick - it does not because Empl ID may also be located on any row. Thank you for the suggestion.
What about this code then...
Code:
Sub MacroTest()
  Dim Report As Worksheet, StartCell As Range, FinalRowReport As Long
  Set Report = Worksheets("Sheet 1")
  
  Set StartCell = Report.UsedRange.Find("Empl ID", LookAt:=xlWhole, MatchCase:=False)
  FinalRowReport = Report.Cells(Rows.Count, StartCell.Column).End(xlUp).Row
  Intersect(StartCell.CurrentRegion, StartCell.Resize(FinalRowReport - StartCell.Row + 1)).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
 
Upvote 0
Rick, Thank you - that looks more promising - however, I need a way to put the Column letter in the .Range Function because I'm actually not just selecting the range in my real code, but using the range to look up and compare values / conditions.

Here is an example of the rest of my code

Code:
For i = 2 To FinalRowReport

If Report.Range("StartColLetter" & i).Value = EmplID Then
                            If PayType = "" Then
                                If Report.Range("AC" & i).Value = CompareDate Then
                                        HrsWrkd = HrsWrkd + Report.Range("D" & i).Value
                                End If

'Please note how I'm using the "StartColLetter" in the range function.
 
Last edited:
Upvote 0
Rick, Thank you - that looks more promising - however, I need a way to put the Column letter in the .Range Function because I'm actually not just selecting the range in my real code, but using the range to look up and compare values / conditions.

Here is an example of the rest of my code

Code:
For i = 2 To FinalRowReport

If Report.[COLOR=#FF0000][B]Range("StartColLetter" & i)[/B][/COLOR].Value = EmplID Then
                            If PayType = "" Then
                                If Report.Range("AC" & i).Value = CompareDate Then
                                        HrsWrkd = HrsWrkd + Report.Range("D" & i).Value
                                End If

'Please note how I'm using the "StartColLetter" in the range function.
Don't use Range, rather, use Cells instead (it can take the column reference either by letter or number)...
Code:
...
...
StartColNumber = Report.UsedRange.Find("Empl ID", LookAt:=xlWhole, MatchCase:=False).Column
If Report.Cells(i, StartColNumber).Value = EmplI Then
... 
...
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top