I need to get the letters of logical column that I have created in my excel sheet.
When I click on a cell ,suppose H7, that has data 23, I am able to get Te excel column H, but for some business purpose I want A, that is the letter in 5th row corresponding to a cell that is selected.If suppose it is 56, I want B.
Image 2 shows why I want B instead of excel column I,it is because if I insert a column my excel column letter will change but the numbers corresponding to the logical column letter in B wil remain in B only.
I am able to get the excel column letters using the code:
Public Function ColumnLetters(rInput As Range) As String
ColumnLetters = Split(rInput.Address, "$") (1)
End Function
If the column letter extracted is H, I need the value that is in 5th row just below it that is A and so on.For I it’s B, and so on.
I have done this so that inserting additional columns ,excel column letters will change but it won’t change my logical column letters.
Can someone please help, please?
As FDavidov has said – you can use named ranges to achieve this if we’re understanding your question correctly.
In the example shown below I have highlighted column A and typed the name RefThisColumn
in the Name Box
(red square). I have also selected just cell A1 and typed RefThisCell
in the Name Box
.
I can then use these named ranges in VBA without having to update references.
After inserting columns to move my named ranges into column E (green square) I ran the exact same code and got the exact same result:
Answer:
You just want to convert a column number to a letter, right?
On your Vba code, use Chr()
. This will convert a number you input to a character.
If you put Chr(65)
it will return “A”. You just need to add your calculations now to reach the letter you want.
I didn’t understand what exactly you want, but as an example, lets say col
is your column number variable.
The Chr(64+col)
would return A for column 1.
However if you wanted to return A for column 5, you would tweak it like Chr(60+col)
Tags: excelexcel