vba code to convert number into a column letter

AlphaRisk

New Member
Joined
Mar 22, 2012
Messages
3
I am using some code which currently converts a number into the corrisponding column letter. eg 1 = A, 2 = B, 27 = AA, 56 = BD etc.

Here is the code:

If mycolumn > 26 Then
Mcl = Chr(Int((mycolumn - 1) / 26) + 64) & Chr(Int((mycolumn - 1) Mod 26) + 65)
Else
Mcl = Chr(mycolumn + 64)
End If

Where "mycolumn" = number & "Mcl" = letter.

This code will only calculate correctly for numbers up to 702 as this is ZZ.
How can i add onto this code to calculate for 3 letter column. eg AAA = 703, 4206 = EFT.

I cant seem to get my head around it.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I rewrite code as below, it works if AlphaColumn was start from A till AAA but failed if test with ZZZ, any idea why it failed ?
Code:
Sub LtrCol()
Dim AlphaColumn As String
Dim ColumnNumber As Long
AlphaColumn = "ZZZ"
ColumnNumber = Columns(AlphaColumn).Column
MsgBox ColumnNumber
End Sub
 
Upvote 0
failed if test with ZZZ, any idea why it failed ?

last column in 2007+ is XFD so it fails because column ZZZ doesn't exist

Edit: seems like an echo of what Andrew stated now.
 
Upvote 0
Anyone could help to rewrite below code so that in case if user enter column letter > XFD, a msgbox will pop up to notify user invalid value.
Code:
If StrColTextBox.Text <> "" Then
  If IsLetter(StrColTextBox.Text) Then
       If Len(StrColTextBox.Text) <= 3 And Columns(StrColTextBox.Text).Column <= 16384 Then
         StrColTextBox.Text = UCase(StrColTextBox.Text)
         UCase (StrColTextBox.Text)
         StrCol = StrColTextBox.Text
       Else
         MsgBox "Please enter a valid value.The valid value should between A to XFD ", vbCritical
         StrColTextBox.Text = ""
       End If
  Else
    MsgBox "Please enter a valid value.The valid value should between A to XFD ", vbCritical
    StrColTextBox.Text = ""
    Cancel = True
  End If
End If
 
Upvote 0
This isn't a valid line of code on its own:

Code:
UCase (StrColTextBox.Text)

I think you mean:
Code:
StrColTextBox.Text = UCase (StrColTextBox.Text)
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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