We help IT Professionals succeed at work.

Get the Extreme Left and Right Cell addresses

Rayne
Rayne asked
on
Hello Everyone,

I am looking for a code that does this. I am tired to manually mentioning the first and the last column header address in all of my functions so need this macro to grab these extreme left – right cell address. Now..
What it does >> it looks at sheet1  - the very first row that has the column names – searches left and right….when it searches left, it grabs c1’ cell address (B3)  - when it searches extreme right, it gets the c9’s cell address and finally give s a msgbox saying :
Left Header Cell : “B3”
Right Header Cell: “J3”

Please note that the first row that has the column names can be at row 3 or any other row…so it has to do some type of search or something
leftrightCells.xlsx
Comment
Watch Question

Cost Accountant
CERTIFIED EXPERT
Top Expert 2012
Commented:
One way to do it:

Sub TopColumns()

Dim uRange As Range
Set uRange = ActiveSheet.UsedRange.Rows(1)

TopLeft = Replace(Left(uRange.Address, InStr(1, uRange.Address, ":", vbTextCompare) - 1), "$", "")
TopRight = Replace(Right(uRange.Address, Len(uRange.Address) - InStr(1, uRange.Address, ":", vbTextCompare)), "$", "")

MsgBox ("Left Header Cell : '" & TopLeft & "'" & vbCrLf & "Right Header Cell: '" & TopRight & "'")

End Sub

Open in new window

NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Rayne

How can you tell which row is the row with headings?
What are the extreme left and extreme right headings called? You said the row would vary but would the columns always be the same?

Author

Commented:
Hello All,

For your excellent feedbacks: I will be opening a second question follow to this and get all other different approaches towards this issue.

The_Barman: Thank your solution works for me. you used used range. I would now want to see if there is nay other approach (s) different from this method.

 
imnorie: How can you tell which row is the row with headings?

The top most row is most likely to be the row with headings - now the top-most row could be on row3 or row 5 or any row

 


techfanatic
 What are the extreme left and extreme right headings called? You said the row would vary but would the columns always be the same?
C1, C9
Actually, the users would be adding in more columns at the end of the table, so the last column will change with time.


I have posted another question for you guys > if you can come up with different approaches to resolve this issue.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.