Consulting

Results 1 to 9 of 9

Thread: Random Variables

  1. #1

    Random Variables

    Hi all,

    I am writing you because I am a bit stuck with the module that I want to create in VBA.

    Basically, I have two sheets. In one of them called "Intervals" I have several columns having different no of cells as seen below:

    L T A V
    200 2 12 0.2
    100 6 10 0.5
    50 5 0.8
    60 1.0

    My intention is to create another sheet called "Input", having n number of combinations of the values from the 4 columns.
    n will be giving as an input by the user via an inputbox.

    Something like:

    n = 4


    200 2 10 0.8
    50 2 5 0.2
    50 6 12 0.2
    60 2 5 1.0

    This is what I have done so far. If someone could help it would be great.

    Thank you.

    Sub Parameters_Input()
      Dim mybook As Workbook, BaseWks As Worksheet, s2 As Worksheet
      Dim myValue As Variant
      Dim Cnum As Long, noCell As Long
      Dim n As Double
      Dim R2 As Range
    
    
      Application.DisplayAlerts = False
         
        'detele
        For q = 1 To Worksheets.Count
           If Worksheets(q).Name = "Input" Then
              Worksheets(q).Delete
              Exit For
              End If
        Next q
        
        Application.DisplayAlerts = True
        
        ' Add a new workbook with one sheet.
        Set mybook = ThisWorkbook
        Set BaseWks = mybook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
        BaseWks.Name = "Input"
        
        myValue = InputBox("Please enter the number of simulations")
        
        Worksheets("Intervals").Activate
        
        Set s2 = Sheets("Input")
        
        Cnum = Range("AZ1").End(xlToLeft).Column
           
         For i = 1 To Cnum
           noCell = Cells(Rows.Count, i).End(xlUp).Row
           Set R2 = s1.Range(Cells(1, 1), Cells(Cnum, myValue))
           .....
               n = Application.WorksheetFunction.RandBetween(Cells(2, i), Cells(noCell, i))
           ....
        Next i
                   
    End Sub
    Last edited by SamT; 12-04-2015 at 06:25 PM. Reason: Used # Icon to add CODE Tags

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to vbax.


    pick 4 columns or Cnum columns?

    Sub vbax_54454_Random_Cells_Values()
     
        Dim SimNum As Long, LastCol As Long
        
        Application.DisplayAlerts = False
        On Error Resume Next
        Worksheets("Input").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
        
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Input"
    
        SimNum = InputBox("Please enter the number of simulations")
        
        With Worksheets("Intervals")
            LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            For i = 1 To SimNum
                For j = 1 To LastCol 'For j = 1 To 4 '?
                    RandRowNo = WorksheetFunction.RandBetween(2, .Cells(.Rows.Count, j).End(xlUp).Row)
                    Worksheets("Input").Cells(i, j).Value = .Cells(RandRowNo, j).Value
                Next j
            Next i
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Newbie JeffK's Avatar
    Joined
    Mar 2009
    Posts
    4
    Location
    g

  4. #4
    VBAX Newbie JeffK's Avatar
    Joined
    Mar 2009
    Posts
    4
    Location
    Excuse the gibberish - ran into a browser glitch.
    JeffK

  5. #5
    Hi mancubus,

    It was number of columns.

    Thank you very much,

    I am in the learning process on VBA, this being my second project.

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome. so it worked for you?

    i missed declaration of i and j

    Sub vbax_54454_Random_Cells_Values() 
         
        Dim SimNum As Long, LastCol As Long, i As Long, j As Long
         
        Application.DisplayAlerts = False 
        On Error Resume Next 
        Worksheets("Input").Delete 
        On Error GoTo 0 
        Application.DisplayAlerts = True 
         
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Input" 
         
        SimNum = InputBox("Please enter the number of simulations") 
         
        With Worksheets("Intervals") 
            LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
            For i = 1 To SimNum 
                For j = 1 To LastCol
                    RandRowNo = WorksheetFunction.RandBetween(2, .Cells(.Rows.Count, j).End(xlUp).Row) 
                    Worksheets("Input").Cells(i, j).Value = .Cells(RandRowNo, j).Value 
                Next j 
            Next i 
        End With 
         
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    Yes it worked.

    Thank you again




    Quote Originally Posted by mancubus View Post
    you are welcome. so it worked for you?

    i missed declaration of i and j

    Sub vbax_54454_Random_Cells_Values() 
         
        Dim SimNum As Long, LastCol As Long, i As Long, j As Long
         
        Application.DisplayAlerts = False 
        On Error Resume Next 
        Worksheets("Input").Delete 
        On Error GoTo 0 
        Application.DisplayAlerts = True 
         
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Input" 
         
        SimNum = InputBox("Please enter the number of simulations") 
         
        With Worksheets("Intervals") 
            LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
            For i = 1 To SimNum 
                For j = 1 To LastCol
                    RandRowNo = WorksheetFunction.RandBetween(2, .Cells(.Rows.Count, j).End(xlUp).Row) 
                    Worksheets("Input").Cells(i, j).Value = .Cells(RandRowNo, j).Value 
                Next j 
            Next i 
        End With 
         
    End Sub

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome again.

    pls mark the thread as solved from thread tool (top right corner of first message) for future references...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Another approach:

    Sub M_snb()
       sn = Sheets("intervals").Cells(1).CurrentRegion
       
       For jj = 1 To UBound(sn, 2)
          sn(1, jj) = Application.Count(Application.Index(sn, [row(1:5)], jj))
       Next
       
       ReDim sp(Val(InputBox("number of iterations")) - 1, 3)
       For j = 0 To UBound(sp)
          For jj = 0 To UBound(sp, 2)
            sp(j, jj) = sn(2 + Int(Rnd * sn(1, jj + 1)), jj + 1)
          Next
       Next
       
       If [not(isref(Input!A1))] Then Sheets.Add.Name = "input"
       Sheets("input").Cells(1).Resize(UBound(sp) + 1, UBound(sp, 2) + 1) = sp
    End Sub

Tags for this Thread

Posting Permissions

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