PDA

View Full Version : [SOLVED:] Creating Array from cells givin error " Subscript out of range"



icemail
01-12-2020, 11:33 AM
Hello i try to create array from cell. But when code coming to "MsgBox myarr(randno)" its giving "'Run-time error '9': Subscript out of range". How can we solved any idea?




Sub randarr
Dim myarr As Variant
Dim myrange As Range
Dim randno As Long

Set myrange = ActiveSheet.Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, 1))
myarr = myrange
randno = Application.WorksheetFunction.RandBetween(LBound(myarr), UBound(myarr))
MsgBox myarr(randno) 'Run-time error '9': Subscript out of range

End Sub

Fluff
01-12-2020, 12:41 PM
As the array is a 2d array you need to use
MsgBox myarr(randno,1)

icemail
01-13-2020, 10:28 AM
its work thank you

Fluff
01-13-2020, 11:12 AM
You're welcome & thanks for the feedback