Random Variable in a predetermined range

lilybezer

New Member
Joined
Sep 29, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have an issue, I need to:
  • Ask the user for the highest and lowest possible values of the random numbers
  • Ask the user how many rows and how many columns she/he wants to create
  • Insert a new sheet into the workbook with the name "‘RandomNumbers"’ and fill this sheet with the appropriate amount of randomly created values. Start in cell A1.

I performed this:

Option Explicit

Sub randomNumbersStandard()
Dim highestValue As Double
Dim lowestValue As Double
Dim columnsInput As Double
Dim rowsInput As Double
Dim cell As Range
Dim rng As Range


highestValue = InputBox("Please enter the highest number of all possible random variable", "Highest Random Number")

lowestValue = InputBox("Please enter the lowest number of all possible random variable", "Lowest Random Variable ")
columnsInput = InputBox("Specify how many columns you want to create", " Columns")
rowsInput = InputBox("Specify how many rows you want to create", " Rows")


Sheets.Add.Name = "Random Numbers"


Set rng = Selection(rowsInput, columnsInput)

For Each cell In rng
cell.Value = WorksheetFunction.RandBetween(lowestValue, highestValue)
Next



End Sub

In this way it only enters a random number in one cell, but I need in a range (from input rows and columns). Could someone give me a hint how can I do this without using selection, because if it askes me to introduce the rows and columns it means that i cannot select the area.
I am not sure if I understood the assignment.

Thank you in advance!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try:
VBA Code:
Set rng = Range(Cells(1, "A"), Cells(rowsInput, columnsInput))
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,977
Members
452,540
Latest member
haasro02

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