Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Excel VBA: Generate random values v2

Hello experts,

I have the following procedure which allows me to generate random values.
Sub Generate_Random_Values()

Dim wb As Workbook
Dim wsRandom As Worksheet
Dim celAddress As String
Dim typeRandom As Long
Dim rngInput As Range
Dim FormulaString As String
Dim shName As String
Dim col As Long

    Application.ScreenUpdating = False
    On Error GoTo Error_Routine

Set wb = ActiveWorkbook

celAddress = InputBox("Enter last range in which you want to apply the procedure.", "Last Cell In The Range!", "V1000")

If celAddress = "" Then
    MsgBox "You didn't enter the last range.", vbExclamation
    Exit Sub
End If

On Error Resume Next
Set rngInput = Range(celAddress)
col = rngInput.Column
On Error GoTo 0

If rngInput Is Nothing Then
    MsgBox "The range you entered is an invalid range.", vbExclamation
    Exit Sub
End If

typeRandom = Application.InputBox("Please input..." & vbNewLine & vbNewLine & _
                    "Enter 1 for Random Numbers." & vbNewLine & _
                    "Enter 2 for Random Letters." & vbNewLine & _
                    "Enter 3 for Random Letter and Numbers.", "What Type Of Random Values?", Type:=1)

If typeRandom = 0 Then
    MsgBox "You din't enter the type of Random Values.", vbExclamation
    Exit Sub
ElseIf typeRandom > 3 Then
    MsgBox "You entered an invalid random type.", vbExclamation
    Exit Sub
End If

Select Case typeRandom
    Case 1
        shName = "Numeric_" & Format(Now, "YYYYMMDD_HHMMSS")
        FormulaString = "=RANDBETWEEN(1,9)&RANDBETWEEN(1,9)"
    Case 2
        shName = "Letters_" & Format(Now, "YYYYMMDD_HHMMSS")
        FormulaString = "=CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))"
    Case 3
        shName = "NL_" & Format(Now, "YYYYMMDD_HHMMSS")
        FormulaString = "=CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))&RANDBETWEEN(1,9)&RANDBETWEEN(1,9)"
End Select

Set wsRandom = wb.Worksheets.Add(after:=wb.Worksheets(wb.Worksheets.Count))
wsRandom.Name = shName

With wsRandom.Range("A2", wsRandom.Range(celAddress))
    .Formula = FormulaString
    .Value = .Value
End With

With wsRandom.Range("A1")
    .Value = "Heading1"
    .AutoFill wsRandom.Range("A1", wsRandom.Cells(1, col)), xlFillDefault
    .CurrentRegion.Columns.AutoFit
End With

Exit Sub
Error_Routine:
    MsgBox Err.Description, vbExclamation, "Something went wrong!"
    Application.ScreenUpdating = True
    
End Sub

Open in new window

When I enter AXX(XX: any number) in first inputbox I have an unexpected error and I have random values created.

User generated image
I would like to avoid this unexpected error but I don't know how to proceed.

Any advice are more than welcome.
SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Thank, you. Unable to test it right now. I will keep you informed.
Subodh,

I tested but I am still having the error and now I don't have the heading.

Please find attached dummy file.
If the best is to start as of column B I can manage this.

Thank you in advance for your help.
NL_20191026_220144_201910261047.xlsx
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Subodh,

I tested again your proposal and it works!
When I read your comment:
https://www.experts-exchange.com/questions/29162273/Excel-VBA-Generate-random-values-v2.html#a42967625
I made a mistake by replacing line 68 instead of line 69.
I have just reported my comment in order to assign your solution.

Thank you again for your great support!
You're welcome Luis! Glad it worked as desired.

I have reported the question and requested to reopen it so that you may select right post/posts as an Answer.
Subodh,
I have just assigned your solution.
Thank you again for your great support!
You're welcome again!