Iterating with letters

guamlet

Board Regular
Joined
Dec 29, 2002
Messages
145
I have an iteration, let's say i=1 to 4. Inside the loop, I'd like it to add a letter (a, then b, then c, etc) to a string each time through the loop. I'd like to do it without having to use If/Then statements for each possible i. Any hints?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You could try converting ascii characters. This will show you how to get a quick list without having to look them up.

Code:
Private Function ShowChar()
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim x As Integer

On Error GoTo HandleErr
Set dbs = CurrentDb()

Set rs = dbs.OpenRecordset("Select * from Table1", dbOpenDynaset)

With rs
  For x = 0 To 255
    .AddNew
    .Fields(0).Value = x
    .Fields(1).Value = Chr(x)
    .Update
  Next x
End With

Set rs = Nothing
Set dbs = Nothing
End Function

Oops, sorry. I did not completely answer the question.
The above just dumps the character value into a two field table called 'Table1'

To use this, you might start the For...Next at the right number sequence (65=A, 66=B...97=a, 98=b) and use a concatenation function.

Something like this would spit out capital letter A-Z
I guess it depends on what you're really doing with this.

For x = 65 to 90
strValue = strValue & Chr(x)
Next x

Mike
 
Upvote 0
The Choose() function lets you pick from an array of choices.
Code:
Dim i As Integer
Dim str As String
For i = 1 To 4
   str = Choose(i, "A", "B", "C", "D")
Next i

HTH

Peter
 
Upvote 0
Thanks. I have gone w/ mdmilner's function using Char(x). It works perfectly!
 
Upvote 0

Forum statistics

Threads
1,221,566
Messages
6,160,525
Members
451,655
Latest member
rugubara

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