VBA WorksheetFunction dynamic randbetween

user2259146

hope all is well.

I am slightly stuck on a VBA function called randbetween in Excel.

Nature of the problem is that I need to create random numbers based on a bunch of other numbers, about 50,000 other numbers in total.

The random numbers I generate must be between 1 and X. X being the other numbers in column D1:D50,000.

As an example: if cell D1 contains the number 5, then I need to create a random number between 1 and 5 in Cell A1. then move on to D2,D3,D4.....etc and create random numbers for each one accordingly, A2,A3,A4...etc.

I tried to use the following but unfortunately the offset part doesn't work. I want to dynamically work through each cell.

the code is as follows:

r = WorksheetFunction.RandBetween(1, Offset(A1, n, 9))
'where n = 2

Most grateful for any help,

chris neilsen

Your use of OFFSET is the wrong syntax. You would need somthing like

Range("A1").Offset(RowOffset, ColumnOffset)

But there is a much better approach to achieve your stated goal. Use Range.FormulaR1C1

Sub Demo()
    Dim rng As Range

    ' Define range
    Set rng = [A1:A50000]

    ' Put formulas into the range
    rng.FormulaR1C1 = "=RANDBETWEEN(1,RC4)"

    'optional, replace formulas with values
    rng.Value = rng.Value

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VBA WorkSheetFunction.Mode of a Dynamic Populated Array

From Dev

How to apply WorksheetFunction.Average to a dynamic array in VBA

From Dev

Trouble with WorksheetFunction in Excel VBA

From Dev

Excel VBA Range RandBetween function returns an error

From Dev

Excel VBA WorksheetFunction.IsError false positive

From Dev

application.worksheetfunction.vlookup in vba does not loop

From Dev

VBA Excel WorksheetFunction.Rank issue

From Dev

Excel vba percentile worksheetfunction function with collection argument

From Dev

usage of WorksheetFunction.Year in excel vba For loop

From Dev

VBA EXCEL: Use value in Listbox as Lookup value in the Match WorksheetFunction

From Dev

How to worksheetfunction.Trim a very long string in VBA?

From Dev

Excel VBA: Error 1004 WorkSheetFunction 'Unable to get Vlookup property"

From Dev

WorksheetFunction.Match multiple sheets Excel VBA error 1004

From Dev

Excel VBA worksheetfunction.transpose returns dimensions but no values

From Dev

How to worksheetfunction.Trim a very long string in VBA?

From Dev

Application.worksheetFunction.match not working correctly VBA Excel

From Dev

Error 1004 - Vlookup in vba - Unable to get the Vlookup property of the WorksheetFunction class

From Dev

Index ALWAYS Out of Range in VBA after using WorksheetFunction.Frequency

From Dev

RANDBETWEEN for SQL Server 2012

From Dev

IF with RANDBETWEEN in same cell

From Dev

Dynamic linking of tables in VBA

From Dev

VBA Dictionary with Dynamic Arrays

From Dev

Dynamic For Loops in VBA

From Dev

Appending a dynamic array in VBA

From Dev

Excel VBA Dynamic Ranges

From Dev

Dynamic filename not working (VBA)

From Dev

VBA dynamic checkboxes positioning

From Dev

Refresh a dynamic range in VBA

From Dev

VBA Dynamic Ranges

Related Related

  1. 1

    VBA WorkSheetFunction.Mode of a Dynamic Populated Array

  2. 2

    How to apply WorksheetFunction.Average to a dynamic array in VBA

  3. 3

    Trouble with WorksheetFunction in Excel VBA

  4. 4

    Excel VBA Range RandBetween function returns an error

  5. 5

    Excel VBA WorksheetFunction.IsError false positive

  6. 6

    application.worksheetfunction.vlookup in vba does not loop

  7. 7

    VBA Excel WorksheetFunction.Rank issue

  8. 8

    Excel vba percentile worksheetfunction function with collection argument

  9. 9

    usage of WorksheetFunction.Year in excel vba For loop

  10. 10

    VBA EXCEL: Use value in Listbox as Lookup value in the Match WorksheetFunction

  11. 11

    How to worksheetfunction.Trim a very long string in VBA?

  12. 12

    Excel VBA: Error 1004 WorkSheetFunction 'Unable to get Vlookup property"

  13. 13

    WorksheetFunction.Match multiple sheets Excel VBA error 1004

  14. 14

    Excel VBA worksheetfunction.transpose returns dimensions but no values

  15. 15

    How to worksheetfunction.Trim a very long string in VBA?

  16. 16

    Application.worksheetFunction.match not working correctly VBA Excel

  17. 17

    Error 1004 - Vlookup in vba - Unable to get the Vlookup property of the WorksheetFunction class

  18. 18

    Index ALWAYS Out of Range in VBA after using WorksheetFunction.Frequency

  19. 19

    RANDBETWEEN for SQL Server 2012

  20. 20

    IF with RANDBETWEEN in same cell

  21. 21

    Dynamic linking of tables in VBA

  22. 22

    VBA Dictionary with Dynamic Arrays

  23. 23

    Dynamic For Loops in VBA

  24. 24

    Appending a dynamic array in VBA

  25. 25

    Excel VBA Dynamic Ranges

  26. 26

    Dynamic filename not working (VBA)

  27. 27

    VBA dynamic checkboxes positioning

  28. 28

    Refresh a dynamic range in VBA

  29. 29

    VBA Dynamic Ranges

HotTag

Archive