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!
 
All - I've narrowed it down to exactly what I'm having problems with - it is the syntax inside the Range function ...

So using the following I am able to get the letter of the Column in which "Empl ID" is located ...

Code:
Sub MacroTest()  
Dim Report As Worksheet, StrtCell As Range 
Set Report = Worksheets("Sheet 1")  
FinalRowReport = Report.Cells(Rows.Count, 1).End(xlUp).Row  

StartCell = Reports.UsedRange.Find("Empl ID", lookat:=xlpart).Address 
StartCol = Split(StartCell, "$")(1)    

End Sub

Using the table example in post #1, the above code will return the following:

StartCell = $B$1
StartCol = B

Can someone tell me how to use this information to feed it into the range function properly? ... here is my attempt:

Code:
Report.Range(StartCell & ":" & StartCol & FinalRowReport)   'This is not working for me

Thank you all very much for your time and help!
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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
... 
...

A yes - that should do the trick. Thank you Rick ... just for educational purposes, would you kindly review my prior post #11 and tell me if there is a solution to what I'm asking with the range syntax? I'm really curious to know if I can get it to work that way.

Thank you again!
 
Upvote 0
All - I've narrowed it down to exactly what I'm having problems with - it is the syntax inside the Range function ...

So using the following I am able to get the letter of the Column in which "Empl ID" is located ...

Code:
Sub MacroTest()  
Dim Report As Worksheet, StrtCell As Range 
Set Report = Worksheets("Sheet 1")  
FinalRowReport = Report.Cells(Rows.Count, 1).End(xlUp).Row  

StartCell = Reports.UsedRange.Find("Empl ID", lookat:=xlpart).Address 
StartCol = Split(StartCell, "$")(1)    

End Sub

Using the table example in post #1, the above code will return the following:

StartCell = $B$1
StartCol = B

Can someone tell me how to use this information to feed it into the range function properly? ... here is my attempt:

Code:
Report.Range(StartCell & ":" & StartCol & FinalRowReport)   'This is not working for me

I solved it! Here is how for anyone who may run into this in the future:

Code:
Sub MacroTest()   
Dim Report As Worksheet, myRange As Range 
Set Report = Worksheets("Sheet 1")   
FinalRowReport = Report.Cells(Rows.Count, 1).End(xlUp).Row    
StartCell = Reports.UsedRange.Find("Empl ID", lookat:=xlpart).Address
StartCol = Split(StartCell, "$")(1)
StartRow = Split(StartCell, "$")(2)    

Let myRange = StartCol & StartRow & ":" & StartCol & FinalRowReport

Report.Range(myRange).Select

End Sub

That worked for me! :) ... Thanks to everyone for your contributions to this thread, especially Rick - working through all of your responses helped me arrive at exactly what I was looking for. Cheers!
 
Upvote 0

Forum statistics

Threads
1,216,193
Messages
6,129,447
Members
449,509
Latest member
ajbooisen

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