FisheriesTech
New Member
- Joined
- Feb 3, 2022
- Messages
- 5
- Office Version
- 2016
- Platform
- Windows
Hey Mr.Excel Smarties out there!!
I have an interesting one here for someone smarter then me (which is probably everyone out there!)
I want to select a random subset of cells from a column. I have the below code to do that. It works great except it consistently selects less then the desired 50 cells. What am I doing wrong? Total VBA newbie here.
Option Explicit
Function RandCell(Rg As Range) As Range
Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function
Sub RandomSelection()
Dim Counter2 As Long
Dim TargetRg As Range
Dim Cell As Range
Range("AF2:AF10000").ClearFormats
Range("AF2", Range("AF2").End(xlDown)).Select
Set TargetRg = Selection
For Counter2 = 1 To 50
Set Cell = RandCell(TargetRg)
Cell.Interior.Color = RGB(0, 255, 0)
Next
End Sub
Thank you in advance for anyone who can help. Let me know if you need more information!
I have an interesting one here for someone smarter then me (which is probably everyone out there!)
I want to select a random subset of cells from a column. I have the below code to do that. It works great except it consistently selects less then the desired 50 cells. What am I doing wrong? Total VBA newbie here.
Option Explicit
Function RandCell(Rg As Range) As Range
Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function
Sub RandomSelection()
Dim Counter2 As Long
Dim TargetRg As Range
Dim Cell As Range
Range("AF2:AF10000").ClearFormats
Range("AF2", Range("AF2").End(xlDown)).Select
Set TargetRg = Selection
For Counter2 = 1 To 50
Set Cell = RandCell(TargetRg)
Cell.Interior.Color = RGB(0, 255, 0)
Next
End Sub
Thank you in advance for anyone who can help. Let me know if you need more information!