Generate test data from range of values using vba

karthik82.vk

New Member
Joined
Aug 21, 2011
Messages
15
Hi,

I am generating random values from a pre specified range using VBA code. I have the pre-specified Items placed in Sheet 1 and the test data will gets generated in sheet 3 which is the output sheet. Below is the VBA Code that I use for generating test data.

Code:
Sub Generate_Product()
Dim myRange As Range
Dim n As Integer
Dim fData As Variant
Dim sData As Variant
Dim iC As Byte
Dim mRand As Byte
sData = Sheet3.Range("F2:G11")

Sheets("Output").Activate

n = Application.InputBox(Prompt:="Please enter total values to generate", Title:="Total Values Count", Type:=1)

ReDim fData(1 To n, 1 To 2)

Set myRange = Application.InputBox(Prompt:="Please Select a Range for inputing date values", Title:="Test Dates Generation", Type:=8)

If myRange Is Nothing Then
Exit Sub
Else
myRange.Select
End If

For iC = 1 To n
mRand = Int(Rnd() * 10) + 1
fData(iC, 1) = sData(mRand, 1)
fData(iC, 2) = sData(mRand, 2)
'iC.Value = fData
Next
myRange.Cells = fData
End Sub

I am facing two problems in this code.


  1. The first problem is when I change the sData = Sheet3.Range("F2:G11") range to anyother value say for example G11 to G6, I am getting the error as Subscription Out of Range. I dont know why this is occurring.
  2. Secondly I am not able to generate values more than 250. If enter values more than 250 I am getting the error Runtime Error 6. Over flow.

Can any one help me with this. Below is my image of sheet3

2gwe1s7.png
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
In this line:

Code:
mRand = Int(Rnd() * 10) + 1

10 is the number of rows in sData. Adjust to suit - you can use UBound(sData, 1).

Also try declaring iC and mRand as Long rather than Byte.
 
Upvote 0

Forum statistics

Threads
1,214,543
Messages
6,120,123
Members
448,947
Latest member
test111

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