Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #3   Report Post  
Dave Peterson
 
Posts: n/a
Default

This is from Chip Pearson:

Function ColLetter(ColNumber As Integer) As String
ColLetter = Left(Cells(1, ColNumber).Address(True, False), 1 - (ColNumber 26))
End Function

And as a worksheet function (with the number in A1):

=SUBSTITUTE(ADDRESS(1,A1,4),"1","")



Gary Brown wrote:

Does anyone know of an easy way of getting the Column Letter if you know the
Column Number?
ie: a function that returns "D" if I input 4.
I've written a UDF but is there a better way?
I've listed my UDF below.
TIA,
--
Gary Brown


'/==============================================/
Private Function ColumnLetterFromNumber(iColNumber As Long) _
As String
'this function converts column number into letters
'this is designed to only work thru ZZ (702 columns),
' however, currently Excel only goes to IV (256 columns)
'Syntax: =ColumnLetterFromNumber(4)
' returns 'D'
'Gary Brown 05/11/2000

Dim str1stLetter As String, str2ndLetter As String

Application.Volatile True

ColumnLetterFromNumber = ""

On Error GoTo err_Function

If iColNumber <= 26 Then
str1stLetter = ""
Else
str1stLetter = Chr(Int(iColNumber / 26.001) + 64)
End If

str2ndLetter = _
Chr((iColNumber - _
(26 * Int(iColNumber / 26.001))) + 64)

ColumnLetterFromNumber = str1stLetter & str2ndLetter

exit_Function:
Exit Function

err_Function:
Debug.Print "Error: " & Err.Number & " - " & Err.Description
ColumnLetterFromNumber = ""
GoTo exit_Function

End Function
'/==============================================/


--

Dave Peterson
  #5   Report Post  
Gary Brown
 
Posts: n/a
Default

I knew there had to be a better way.
Leave it to Chip.
Thanks!
--
Gary Brown

Please rate this posting if it is helpful to you.


"Dave Peterson" wrote:

This is from Chip Pearson:

Function ColLetter(ColNumber As Integer) As String
ColLetter = Left(Cells(1, ColNumber).Address(True, False), 1 - (ColNumber 26))
End Function

And as a worksheet function (with the number in A1):

=SUBSTITUTE(ADDRESS(1,A1,4),"1","")



Gary Brown wrote:

Does anyone know of an easy way of getting the Column Letter if you know the
Column Number?
ie: a function that returns "D" if I input 4.
I've written a UDF but is there a better way?
I've listed my UDF below.
TIA,
--
Gary Brown


'/==============================================/
Private Function ColumnLetterFromNumber(iColNumber As Long) _
As String
'this function converts column number into letters
'this is designed to only work thru ZZ (702 columns),
' however, currently Excel only goes to IV (256 columns)
'Syntax: =ColumnLetterFromNumber(4)
' returns 'D'
'Gary Brown 05/11/2000

Dim str1stLetter As String, str2ndLetter As String

Application.Volatile True

ColumnLetterFromNumber = ""

On Error GoTo err_Function

If iColNumber <= 26 Then
str1stLetter = ""
Else
str1stLetter = Chr(Int(iColNumber / 26.001) + 64)
End If

str2ndLetter = _
Chr((iColNumber - _
(26 * Int(iColNumber / 26.001))) + 64)

ColumnLetterFromNumber = str1stLetter & str2ndLetter

exit_Function:
Exit Function

err_Function:
Debug.Print "Error: " & Err.Number & " - " & Err.Description
ColumnLetterFromNumber = ""
GoTo exit_Function

End Function
'/==============================================/


--

Dave Peterson



  #6   Report Post  
Jason Morin
 
Posts: n/a
Default

Nice! I like the worksheet formula. I've seen some long ugly formula to
convert numbers to column letters and vice-versa.

Jason

"Dave Peterson" wrote:

This is from Chip Pearson:

Function ColLetter(ColNumber As Integer) As String
ColLetter = Left(Cells(1, ColNumber).Address(True, False), 1 - (ColNumber 26))
End Function

And as a worksheet function (with the number in A1):

=SUBSTITUTE(ADDRESS(1,A1,4),"1","")



Gary Brown wrote:

Does anyone know of an easy way of getting the Column Letter if you know the
Column Number?
ie: a function that returns "D" if I input 4.
I've written a UDF but is there a better way?
I've listed my UDF below.
TIA,
--
Gary Brown


'/==============================================/
Private Function ColumnLetterFromNumber(iColNumber As Long) _
As String
'this function converts column number into letters
'this is designed to only work thru ZZ (702 columns),
' however, currently Excel only goes to IV (256 columns)
'Syntax: =ColumnLetterFromNumber(4)
' returns 'D'
'Gary Brown 05/11/2000

Dim str1stLetter As String, str2ndLetter As String

Application.Volatile True

ColumnLetterFromNumber = ""

On Error GoTo err_Function

If iColNumber <= 26 Then
str1stLetter = ""
Else
str1stLetter = Chr(Int(iColNumber / 26.001) + 64)
End If

str2ndLetter = _
Chr((iColNumber - _
(26 * Int(iColNumber / 26.001))) + 64)

ColumnLetterFromNumber = str1stLetter & str2ndLetter

exit_Function:
Exit Function

err_Function:
Debug.Print "Error: " & Err.Number & " - " & Err.Description
ColumnLetterFromNumber = ""
GoTo exit_Function

End Function
'/==============================================/


--

Dave Peterson

  #7   Report Post  
Myrna Larson
 
Posts: n/a
Default

Remember that will only work if you are talking about columns A:Z. It will
fail on AA and beyond.

On Fri, 10 Jun 2005 06:42:04 -0700, "Gary Brown"
m wrote:

Brilliant :O
Thanks.


  #8   Report Post  
Dave Peterson
 
Posts: n/a
Default

That's pretty high praise coming from you. Thanks.

Or even a blind squirrel finds a nut now and then!



Jason Morin wrote:

Nice! I like the worksheet formula. I've seen some long ugly formula to
convert numbers to column letters and vice-versa.

Jason

"Dave Peterson" wrote:

This is from Chip Pearson:

Function ColLetter(ColNumber As Integer) As String
ColLetter = Left(Cells(1, ColNumber).Address(True, False), 1 - (ColNumber 26))
End Function

And as a worksheet function (with the number in A1):

=SUBSTITUTE(ADDRESS(1,A1,4),"1","")



Gary Brown wrote:

Does anyone know of an easy way of getting the Column Letter if you know the
Column Number?
ie: a function that returns "D" if I input 4.
I've written a UDF but is there a better way?
I've listed my UDF below.
TIA,
--
Gary Brown


'/==============================================/
Private Function ColumnLetterFromNumber(iColNumber As Long) _
As String
'this function converts column number into letters
'this is designed to only work thru ZZ (702 columns),
' however, currently Excel only goes to IV (256 columns)
'Syntax: =ColumnLetterFromNumber(4)
' returns 'D'
'Gary Brown 05/11/2000

Dim str1stLetter As String, str2ndLetter As String

Application.Volatile True

ColumnLetterFromNumber = ""

On Error GoTo err_Function

If iColNumber <= 26 Then
str1stLetter = ""
Else
str1stLetter = Chr(Int(iColNumber / 26.001) + 64)
End If

str2ndLetter = _
Chr((iColNumber - _
(26 * Int(iColNumber / 26.001))) + 64)

ColumnLetterFromNumber = str1stLetter & str2ndLetter

exit_Function:
Exit Function

err_Function:
Debug.Print "Error: " & Err.Number & " - " & Err.Description
ColumnLetterFromNumber = ""
GoTo exit_Function

End Function
'/==============================================/


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add row total when cell contains both number and letter . cbarcroft Excel Discussion (Misc queries) 4 April 18th 05 11:52 PM
How do I sort a column a unique number? ChelleA Excel Worksheet Functions 7 February 19th 05 10:38 AM
Count number of occurences in 1 column only if something in anothe Wenster Excel Worksheet Functions 2 February 7th 05 09:58 PM
how do I find an average number of specific words in a column cashgrfx New Users to Excel 7 January 6th 05 04:44 PM
GET.CELL Biff Excel Worksheet Functions 2 November 24th 04 07:16 PM


All times are GMT +1. The time now is 09:55 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"