fill a range with numbers - vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,565
Hi
The code below is going to fill a range with random number but all the cells will get the same number. What I want, is to fill cells of that range with different random number. So I am thinking to put that line of code in a loop but how can I loop when user might enter any different range? How you would do that please. Thank you very much.
Code:
Sub cells_fill()
    Dim x as integer
    Dim y As Integer
    Dim n As Integer
    Dim m As Integer
    x = InputBox("row1")
    y = InputBox("col1")
    n = InputBox("row2")
    m = InputBox("col2")
    Range(Cells(x, y), Cells(n, m)).Value = WorksheetFunction.RandBetween(1, 100)
End Sub
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,935
Office Version
2013
Platform
Windows
Try

Code:
Sub MM1()
Dim x As Integer, y As Integer
Dim n As Integer, m As Integer, cell As Range
x = InputBox("row1")
y = InputBox("col1")
n = InputBox("row2")
m = InputBox("col2")
For Each cell In Range(Cells(x, y), Cells(n, m))
    cell.Value = WorksheetFunction.RandBetween(1, 100)
Next cell
End Sub
 

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,565
Thank you very much. Never thought of for-each. So happy to know it. thank you again.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,613
Or perhaps one dialog box and no loop.

Code:
Dim RangeToFill As Range

On Error Resume Next
Set RangeToFill = Application.InputBox("Select a range to fill with the mouse", Type:=8)
On Error Goto 0

If RangeToFill Is Nothing then Exit Sub: Rem cancel pressed

With RangeToFill
    .Formula = "=RANDBETWEEN(1, 100)"
    .Value = .Value
End With
 
Last edited:

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,565
Wow, thanks Mike. So interesting to learn different style. Thanks a lot again
 

Watch MrExcel Video

Forum statistics

Threads
1,101,787
Messages
5,482,925
Members
407,368
Latest member
FunkyFriedChicken

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top