Random string

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
I have this code which generates a string of letters and digits
The code do it in A1
I want to edit the code to include the range("A1:A10")
Code:
Dim intCnt As Integer
    Dim intChr As Integer
    Dim strTemp As String
    Randomize
    For intCnt = 1 To 10
        intChr = Int(Rnd() * (122 - 49)) + 49
        If intChr > 57 And intChr < 97 Then
            intCnt = intCnt - 1
        Else
           strTemp = strTemp & Chr(intChr)
        End If
    Next intCnt
    Range("A1").Value = strTemp
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
YasserKhalil,


Sample worksheet before the macro:


Excel Workbook
A
1
2
3
4
5
6
7
8
9
10
11
Sheet1





After the macro:


Excel Workbook
A
16vl8p767rt
2hgyilnq1w4
3as857hqkol
4g4gv64elc2
59tk4qdho2o
63pt1agiq1y
73m42h5gr28
87pyhhjlqmn
9y54v9koigu
10j914cvxfsr
11
Sheet1





If you run the macro again:


Excel Workbook
A
15mtlnstpgl
2cschxkpd8s
3sw3xoqc3vf
4hd41vehmhw
5d2bsonuqot
6cdrgf2ourt
7vo1jw6wsxh
8clbrl3ej9b
9qva57mwlkw
105gketyajw1
11
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub GenerateString()
' hiker95, 08/02/2011
' http://www.mrexcel.com/forum/newreply.php?do=newreply&noquote=1&p=2811682
Dim a As Long
Dim intCnt As Integer
Dim intChr As Integer
Dim strTemp As String
Application.ScreenUpdating = False
Randomize
For a = 1 To 10 Step 1
  strTemp = ""
  For intCnt = 1 To 10
    intChr = Int(Rnd() * (122 - 49)) + 49
    If intChr > 57 And intChr < 97 Then
      intCnt = intCnt - 1
    Else
      strTemp = strTemp & Chr(intChr)
    End If
  Next intCnt
  Range("A" & a).Value = strTemp
Next a
Application.ScreenUpdating = True
End Sub


Then run the GenerateString macro.
 
Upvote 0
salam Yaser
try this code
Code:
Sub Yaser()
Dim intCnt As Integer
    Dim intChr As Integer
    Dim strTemp As String
    Randomize
    For Each c In Range("a1:a10")
    For intCnt = 1 To 10
        intChr = Int(Rnd() * (122 - 49)) + 49
        If intChr > 57 And intChr < 97 Then
            intCnt = intCnt - 1
        Else
           strTemp = strTemp & Chr(intChr)
        End If
    Next intCnt
    c.Value = strTemp
    strTemp = ""
    Next c
End Sub
 
Upvote 0
I believe there is a lot i need to learn about excel! I don't know a zilch about how it works!

follow this steps from hiker post
Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
 
Last edited by a moderator:
Upvote 0
try..

Code:
Sub Foo()
Dim intCnt As Integer
Dim intChr As Integer
Dim intNum As Integer
Dim strTemp As String
Randomize
Range("A1").Select
For intNum = 1 To 10         'Cell Range to fill
    For intCnt = 1 To 10
        intChr = Int(Rnd() * (122 - 49)) + 49
        If intChr > 57 And intChr < 97 Then
            intCnt = intCnt - 1
        Else
           strTemp = strTemp & Chr(intChr)
        End If
    Next intCnt
Cells(intNum, 1).Value = strTemp
strTemp = ""
Cells(intNum + 1, 1).Select
Next intNum
Range("A1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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