Edmond
01-30-2019, 03:32 PM
Hi Everyone,
My issue here is more about understanding. I am using that formula that I have modified:
Function RandCell(Rg As Range) As Range
Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function
Sub RandCellTest()
Dim Counter As Long
Dim TargetRg As Range, Cell As Range
Dim LastRow As Integer
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Set TargetRg = Range(Cells(2, 1), Cells(LastRow - 1, 1))
For Counter = 1 To 50
Set Cell = RandCell(TargetRg)
Range(Cell, Cell.Offset(, 3)).Select
Next
End Sub
It works well. However I have some trouble figuring the logic behind the way is written the function.
1st: Why do we have to put this rg (rg.cells)? Plus we don't define it. Is it related to the VBA Function?
2nd: How Int(Rnd * Rg.Cells.Count) gives a number?
It seems obvious but I can't make it...
Thanks in advance for your help!
My issue here is more about understanding. I am using that formula that I have modified:
Function RandCell(Rg As Range) As Range
Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function
Sub RandCellTest()
Dim Counter As Long
Dim TargetRg As Range, Cell As Range
Dim LastRow As Integer
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Set TargetRg = Range(Cells(2, 1), Cells(LastRow - 1, 1))
For Counter = 1 To 50
Set Cell = RandCell(TargetRg)
Range(Cell, Cell.Offset(, 3)).Select
Next
End Sub
It works well. However I have some trouble figuring the logic behind the way is written the function.
1st: Why do we have to put this rg (rg.cells)? Plus we don't define it. Is it related to the VBA Function?
2nd: How Int(Rnd * Rg.Cells.Count) gives a number?
It seems obvious but I can't make it...
Thanks in advance for your help!