Results 1 to 12 of 12

Thread: Change Column Number to Column Title

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Change Column Number to Column Title

    I have used the following to get the last used column in a row, but is there any quick way to transfer this number into the column letters, other than a huge select case or similar. I'm sure there must be, but can't find anything so far
    E.G. LastCol = 34
    LastCol = AH

    For me the LastCol would be "LastCol - 1" as the last column in the row is optional.

    VB Code:
    1. LastCol = ActiveSheet.Range("IV1").End(xlToLeft).Column
    Last edited by aikidokid; Feb 9th, 2007 at 09:10 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Change Column Number to Column Title

    Found this on another thread here a while ago, can not remember the thread
    but it works for me.

    Formula is buggy, I have posted a corrected/working version below in Post No 12

    VB Code:
    1. Public Function GetColumnLetters(ByVal I As Long) As String
    2.     If I > 0 And I < 27 Then
    3.         GetColumnLetters = Chr$(64 + I)
    4.     ElseIf I > 26 And I < 257 Then
    5.         GetColumnLetters = Chr$(64 + ((I - (I Mod 26)) / 26)) & Chr$(64 + (I Mod 26))
    6.     Else
    7.         GetColumnLetters = ""
    8.     End If
    9. End Function

    Last edited by Torc; Apr 25th, 2007 at 06:41 AM. Reason: Formula Incorrect Posted Correct Below
    Signature Under Construction

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Change Column Number to Column Title

    Thanks torc, thats works lovely.

    I am getting a Type mismatch on this part though.
    Any ideas?
    VB Code:
    1. LastCol = ActiveSheet.Range("IV1").End(xlToLeft).Column
    2. ColLetters = GetColumnLetters(LastCol)
    3. If Range("A" & RowNum : ColLetters & LastCol) = "" Then [COLOR=DarkRed]<----------- HERE[/COLOR]
    4.     MsgBox "empty cells exist in this row"
    5.     Exit Sub
    6. Else
    7.     MsgBox "Completed row"
    8.     'change the formatting of the first cell in this row
    9.     Cells(RowNum, 1).Interior.ColorIndex = 1
    10.     Cells(RowNum, 1).Font.ColorIndex = 2
    11.     Cells(RowNum, 1).Font.Bold = True
    12. End If
    Last edited by aikidokid; Feb 9th, 2007 at 09:36 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Change Column Number to Column Title

    does this help?

    Hi

    Paste this code in a module...

    VB Code:
    1. Function ReturnName(ByVal num As Integer) As String
    2.         ReturnName = Split(Cells(, num).Address, "$")(1)
    3.     End Function

    In any cell insert this formula "=ReturnName(34)" . this will give you "AH"

    You can also use this formula in vba for example

    VB Code:
    1. 'this will give you AH
    2. MsgBox ReturnName(34)
    Hope this helps...
    Last edited by Siddharth Rout; Feb 9th, 2007 at 11:26 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Change Column Number to Column Title

    Hi koolsid

    I came across this and it seems to work fine.
    VB Code:
    1. Public Function GetColumnLetters(ByVal I As Long) As String
    2.     If I > 0 And I < 27 Then
    3.         GetColumnLetters = Chr$(64 + I)
    4.     ElseIf I > 26 And I < 257 Then
    5.         GetColumnLetters = Chr$(64 + ((I - (I Mod 26)) / 26)) & Chr$(64 + (I Mod 26))
    6.     Else
    7.         GetColumnLetters = ""
    8.     End If
    9. End Function
    As I have the number of columns that the sheet has used I just use this and it gives me the column letters.

    What I am still not too sure about is how to loop through the whole of the row checking if any of the cells are empty.

    Once I have done this, then I will send you the sheet ... so far
    Last edited by aikidokid; Feb 9th, 2007 at 12:12 PM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Change Column Number to Column Title

    Yes but you need to paste both the functions in the module and my function is a three line code. You make the choice
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Change Column Number to Column Title

    So to call you function in VBA, which is what I am doing it would be..
    VB Code:
    1. ColLetters = ReturnName(LastCol - 1)

    Did you see my edit on the last post?

    PS
    By the way, if you don't declare your function as Private or Public, does that mean it will be considered as Public?
    Last edited by aikidokid; Feb 9th, 2007 at 12:26 PM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Change Column Number to Column Title

    What I am still not too sure about is how to loop through the whole of the row checking if any of the cells are empty.
    Does this help?

    VB Code:
    1. Private Sub CommandButton1_Click()
    2. 'gives you the Col No of last cell in your range
    3. last_cell_column = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Column
    4.  
    5. 'count for empty cells
    6. Count = 0
    7.  
    8. 'looping thru the cells
    9. For i = 1 To last_cell_column
    10.     'assuming you want to loop thru row 1
    11.     If Cells(1, i).Value = "" Then
    12.         Count = Count + 1
    13.     End If
    14. Next i
    15.  
    16. MsgBox "You have " & Count & "cells empty"
    17.  
    18. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Change Column Number to Column Title

    So to call you function in VBA, which is what I am doing it would be..
    visual basic code:ColLetters = ReturnName(LastCol - 1)
    Did you see my edit on the last post?
    If your last column is 34 and you want the name of that column then Why (LastCol - 1)? why not (lastcol)? I can answer the question but i want you to understand the logic... try the code if you want. Also understand what the function ReturnName() does...

    On a humorous note try this

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.   'last col no
    3.   Colno = 34
    4.   MsgBox "Which is the one that you want: " & ReturnName(Colno) & " or " & ReturnName(Colno - 1)
    5. End Sub

    Hope this helps....
    Last edited by Siddharth Rout; Feb 9th, 2007 at 12:37 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Change Column Number to Column Title

    I should have explained the LastCol - 1
    The last column in the row is optional, so the used doesn't have to necessarily enter anything, so if it's empty the records could still be complete.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Change Column Number to Column Title

    I should have explained the LastCol - 1
    The last column in the row is optional, so the used doesn't have to necessarily enter anything, so if it's empty the records could still be complete.
    In that case yes,

    VB Code:
    1. ColLetters = ReturnName(LastCol - 1) 'which will give you "AG"

    This is correct
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Change Column Number to Column Title

    Just discovered that the formula I posted above has a flaw.

    For Column number 52 It returns B@, and for Column 78 It returns C@ etc.

    The code below is a modified version of Si's code in http://www.vbforums.com/showthread.php?t=319820

    I have tested it for all numbers from 1 to 256 and it retruns the correct column code.

    VB Code:
    1. Public Function GetColumnLetters(ByVal I As Long) As String
    2.     If I > 0 And I < 27 Then
    3.         GetColumnLetters = Chr$(64 + I)
    4.     ElseIf I > 26 And I < 257 Then
    5.         GetColumnLetters = Chr$(Int((I - 1) / 26) + 64) & _
    6.                      Chr$(((I - 1) Mod 26) + 1 + 64)
    7.     Else
    8.         GetColumnLetters = ""
    9.     End If
    10. End Function
    Last edited by Torc; Apr 25th, 2007 at 06:39 AM. Reason: Added link to earlier query.
    Signature Under Construction

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width