VBA Collections value - Run time error 9 - subscript out of range

  • Thread starter Thread starter Legacy 502047
  • Start date Start date
L

Legacy 502047

Guest
Hi everyone,

I'm new to collections and I keep getting a "subscript out of range" error on the underlined bit of code when I'm trying to set the value of the cell, wondering where I'm going wrong here?

Sub GetRandomEmptyCell()
Dim i, j, randomcell As Integer
Dim allcells As New Collection

For i = 1 to 10
For j = 1 to 4
If Cells(i, j) = "" Then
allcells.Add Cells(i, j)
End If
Next j
Next i

If allcells.Count >= 1 Then
randomcell = Rnd * allcells.Count
allcells(randomcell).Value = 2
allcells.Remove randomcell
End If
End Sub

Thanks for your help
yuro0804
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
The indexing for a collection starts at 1, not 0. Therefore, try the following instead...

VBA Code:
randomcell = (Rnd * allcells.Count) + 1

Hope this helps!
 
Upvote 0
The indexing for a collection starts at 1, not 0. Therefore, try the following instead...

VBA Code:
randomcell = (Rnd * allcells.Count) + 1

Hope this helps!
Hi Domenic, thanks for your reply, unfortunately it hasn't fixed the error but still good to know, thanks!
 
Upvote 0
Try

VBA Code:
If allcells.Count >= 1 Then
   randomcell = Rnd * allcells.Count
   allcells(randomcell) = 2
   Debug.Print allcells(randomcell)
   allcells.Remove randomcell
 End If
 
Upvote 0
I missed it earlier, but you need to use the Int function on the result before assigning it to your variable...

VBA Code:
randomcell = Int(Rnd * allcells.Count) + 1

The Int function removes the fractional part of the number and returns the resulting integer. Whereas assigning the result to an integer variable rounds the number to the nearest integer.

Hope this helps!
 
Last edited:
Upvote 0
Solution
I missed it earlier, but you need to use the Int function on the result before assigning it to your variable...

VBA Code:
randomcell = Int(Rnd * allcells.Count) + 1

The Int function removes the fractional part of the number and returns the resulting integer. Whereas assigning the result to an integer variable rounds the number to the nearest integer.

Hope this helps!
Ah that makes sense, perfect, seems to have fixed it! Thanks so much :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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