+ Reply to Thread
Results 1 to 3 of 3

Generate test data from range of values using vba

Hybrid View

  1. #1
    Registered User
    Join Date
    04-15-2014
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    4

    Generate test data from range of values using vba

    Hi,

    I am new to excel vba and i am working on a request. I want to randomize a range of values stored in sheet 1 and insert them in another cell.

    I am having values for sales consisting of product names and its price. there are 10 product names and its corresponding prices and is stored in cells E1:F10. I want to generate test data containing product names and its corresponding price. The generated data needs to be saved in the cells A1:B50

    the product names needs to be randomized. This needs to be done in vba. Below is the screenshot of the final result that is needed. As you can see, I have randomized my 10 products and its prices into the test data column. This was done using vlookup formula and I need the same to be done in vba

    Untitled.png

    Any help is highly appreciable.

    Regards,
    KV

  2. #2
    Forum Contributor
    Join Date
    01-25-2011
    Location
    Virginia
    MS-Off Ver
    Excel 2016
    Posts
    166

    Re: Generate test data from range of values using vba

    Assuming the header data for the two tables starts in A1 and E1 respectively (i.e. as in the picture the actual data starts in A3 and E3), this will work:
    Option Explicit
    Option Base 1
    Sub GenerateTestData()
    
    Dim vSourceDataArray As Variant
    Dim vRandProdsArray(1 To 50, 2) As Variant
    Dim iMaxProdsArray(1 To 10) As Integer
    Dim rTestData As Range
    Dim n As Integer
    Dim m As Integer
    
    vSourceDataArray = Range("E3:G13").Value 'Transfer data to array
    
    For n = 1 To 50 'Fill array based on random number generator
    m = Int(10 * Rnd + 1) 'Generate random number 1 to 10
    iMaxProdsArray(m) = iMaxProdsArray(m) + 1
    If iMaxProdsArray(m) > 5 Then 'If random number used 5 times or more reset n and try again
      n = n - 1
    Else 'Fill array with product values
      vRandProdsArray(n, 1) = vSourceDataArray(m, 2)
      vRandProdsArray(n, 2) = vSourceDataArray(m, 3)
    End If
    Next n
    
    Set rTestData = Range("a3:b52") 'Copy random products array to spreadsheet
    rTestData = vRandProdsArray
    
    End Sub
    Tom

  3. #3
    Registered User
    Join Date
    04-15-2014
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Generate test data from range of values using vba

    Hi,

    Thanks for the code and helping me.

    -KV

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 08-21-2013, 07:10 PM
  2. [SOLVED] Test if a cell is within a name range and return a text value based on the test
    By DraconR in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-24-2013, 02:46 AM
  3. Replies: 3
    Last Post: 09-26-2012, 09:48 AM
  4. =IF using a range of cells for logical test and values
    By bendoyle1983 in forum Excel General
    Replies: 4
    Last Post: 05-25-2010, 09:16 PM
  5. [SOLVED] How do I generate a list from a range of values
    By robo7084 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-05-2006, 08:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1