Results 1 to 5 of 5

Thread: Formula needed please (Excel VBA)

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Formula needed please (Excel VBA)

    Can't for the life of me think of how to get round this . Here goes ...
    | A | B | C |
    ---------------------
    | 1 | 2 | 3 |
    In Excel, I can use the following on the above :
    Code:
    Dim intCounter as integer, strMessage as String
    
    For intcounter = 1 to ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
        strMessage = strMessage & Range(chr(intcounter +64) & "1") & " "
    Next
     
    Msgbox strMessage
    This takes the intcounter number(index of the selected column) & converts its Ascii value to a character - the column letter (A - Z).
    This crashes though, when columns past Z are reached (getting into AA, AB, BA, BB, AAA etc). Can anyone think of a formula or loop I can put in to allow for these extra columns please ?

    Thanks guys !

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334
    Try this


    VB Code:
    1. Dim intCounter as integer, strMessage as String, intOverCounter as Integer
    2. Dim intOverCounter as Integer, intModAlpha as Integer
    3.  
    4. Const LASTALPHA = 26
    5.  
    6. For intcounter = 1 to ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
    7.  
    8.     If intCounter > LASTALPHA Then
    9.         If intCounter > LASTALPHA Then
    10.              intModAlpha = intCounter Mod LASTALPHA
    11.              If intModAlpha = 0 Then
    12.                   intOverCounter = intCounter / LASTALPHA
    13.              Else
    14.                   intOverCounter = ((intCounter - (intCounter Mod LASTALPHA)) / LASTALPHA)
    15.              End If
    16.         End If
    17.  
    18.         strMessage = strMessage & Range(chr((intOverCounter) +64) & "1") & Range(chr((intcounter- (LASTALPHA*intOverCounter) +64) & "1")  & " "
    19. Next
    20.  
    21. Msgbox strMessage

    I may have misinterpreted the strMessage bit, but basically you need to have two bits if the intCounter is over 26 (ie Z)
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  3. #3
    Si_the_geek
    Guest
    Easiest way is not to use "range" but "cells" instead, as this doesn't need the letters!

    VB Code:
    1. Dim intCounter as integer, strMessage as String
    2.  
    3. For intcounter = 1 to ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
    4.     strMessage = strMessage & cells(1,intcounter) & " "
    5. Next
    6.  
    7. Msgbox strMessage

  4. #4
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334
    OK so I did misinterpret that bit . . .

    VB Code:
    1. Dim intCounter as integer, strMessage as String, intOverCounter as Integer
    2. Dim intOverCounter as Integer, intModAlpha as Integer
    3.  
    4. Const LASTALPHA = 26
    5.  
    6. For intcounter = 1 to ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
    7.  
    8.     If intCounter > LASTALPHA Then
    9.         If intCounter > LASTALPHA Then
    10.              intModAlpha = intCounter Mod LASTALPHA
    11.              If intModAlpha = 0 Then
    12.                   intOverCounter = intCounter / LASTALPHA
    13.              Else
    14.                   intOverCounter = ((intCounter - (intCounter Mod LASTALPHA)) / LASTALPHA)
    15.              End If
    16.         End If
    17.  
    18.         If intOverCounter Then
    19.              strMessage = strMessage & Range(chr$((intOverCounter) +64) & chr$((intcounter- (LASTALPHA*intOverCounter) +64) & "1") & " "
    20.         Else
    21.              strMessage = strMessage & Range(chr(intcounter +64) & "1") & " "
    22.         End If
    23.  
    24. Next
    25.  
    26. Msgbox strMessage

    Although I would go with the Cells option personally . . . I just wanted to clarify what I wass saying.
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  5. #5

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I do like making things difficult for myself...
    Thanks all, those both work great !

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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