I am writing a VBA code which loops through a defined range in a worksheet, finds the filled cells within its limits and assigns the x,y values of each cell into an array.
This is my code:
After the first round, I get an error '9' - subscript out of range.
I have looked anywhere over the internet, and stumbled upon ReDim Preserve, and Transpose (which I'm not familiar with), and tried everything I could, but I just can't get to settle the problem.
I'd appreciate any help on how to rewrite my code.
This is my code:
Code:
Sub FindCells()
Dim lWidth as Integer, lHeight as Integer 'both of these parameters are subject to change by user's choice
'and can get the values of 10 to 500 and define the range (rows and columns)
Dim Molecules() As Variant
ReDim Molecules(1 To lHeight, 1 To lWidth)
For Y = LBound(Molecules, 1) To UBound(Molecules, 1) 'rows (y)
For x = LBound(Molecules, 2) To UBound(Molecules, 2) 'columns (x)
If Cells(Y, x).Interior.ColorIndex <> xlNone Then
Molecules = Array(Y, x)
MsgBox "the address of original cells is = " & Molecules(1) & ", " & Molecules(2)
End If
Next x
Next Y
End Sub
After the first round, I get an error '9' - subscript out of range.
I have looked anywhere over the internet, and stumbled upon ReDim Preserve, and Transpose (which I'm not familiar with), and tried everything I could, but I just can't get to settle the problem.
I'd appreciate any help on how to rewrite my code.