Using "A-Z" like "0-9" without defining individual fields

alktrigger

Aimless Extraordinaire
Local time
Yesterday, 19:04
Joined
Jun 9, 2009
Messages
124
I'm creating a report in excel that needs to adapt to the output. Is there a way to count letters of the alphabet (a-b..y-z) similar to using regular numerical variables?

My current approach is as follows. It is not implemented yet since I am hoping there is a better way
Code:
Dim strChar(1 to 26) As String

strChar(1) = "A"
strChar(2) = "B"
strChar(3) = "C"
   ------
strChar(25) = "Y"
strChar(26) = "Z"

I am hoping there is a way to replace the 26 individual lines defining characters with a Do-Loop that does that. Any suggestions?
 
Ok, well I guess there was no answers to this... so this was my final solution.

In my module:
Code:
Public Function AlphaDefine(intCount As Integer)

    Select Case intCount
        Case 0
            AlphaDefine = "A"
        Case 1
            AlphaDefine = "B"
        Case 2
            AlphaDefine = "C"
        Case 3
            AlphaDefine = "D"
        Case 4
            AlphaDefine = "E"
        Case 5
            AlphaDefine = "F"
        Case 6
            AlphaDefine = "G"
        Case 7
            AlphaDefine = "H"
        Case 8
            AlphaDefine = "I"
        Case 9
            AlphaDefine = "J"
        Case 10
            AlphaDefine = "K"
        Case 11
            AlphaDefine = "L"
        Case 12
            AlphaDefine = "M"
        Case 13
            AlphaDefine = "N"
        Case 14
            AlphaDefine = "O"
        Case 15
            AlphaDefine = "P"
        Case 16
            AlphaDefine = "Q"
        Case 17
            AlphaDefine = "R"
        Case 18
            AlphaDefine = "S"
        Case 19
            AlphaDefine = "T"
        Case 20
            AlphaDefine = "U"
        Case 21
            AlphaDefine = "V"
        Case 22
            AlphaDefine = "W"
        Case 23
            AlphaDefine = "X"
        Case 24
            AlphaDefine = "Y"
        Case 25
            AlphaDefine = "Z"
        End Select
        

End Function

Then I implemented it with:
Code:
 Dim strChar(25) As String
    Dim n As Integer
    
'fill strChar() array
    n = 0
    For n = 0 To 25
        strChar(n) = AlphaDefine(n)
    Next
 
I'd have done something like:

Dim strAlpha as String
Dim n as Integer
strAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
For n=0 to 25
strchar(n) = Mid(strAlpha,n+1,1)
Next

Either that, or do away with the strchar array altogether, and just use strAlpha and the Mid function directly where required.
 
Alternatively...

Dim n as Integer
For n=0 to 25
strchar(n) = Chr(n+65)
Next

-I'm not sure though, if the Chr() function ever throws any surprises in contexts such as different regional/language settings.
 
I like that method too actually, I'm actually going to swap that out for the case-select function. Thanks.


Code:
refering to Dim n as Integer
strAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
For n=0 to 25
strchar(n) = Mid(strAlpha,n+1,1)
 
Last edited:

Users who are viewing this thread

Back
Top Bottom