0

Could anyone help me to figure out why this code erro is "subscript is out of range". (run time error 9)

Dim arrayU() As Variant
Dim arrayX() As Variant
Dim arrayW() As Variant
Dim LrowU As Integer
Dim LrowX As Integer
Dim LrowW As Integer
Dim i As Integer
Dim j As Integer
Dim bed_in_use As Integer


   For i = 3 To LrowX
   For r = 3 To LrowW
   For j = 3 To LrowX

   If bed_in_use >= 24 Then Exit For

   If arrayX(i) = arrayW(r) Then
   bed_in_use = bed_in_use - 1
   If arrayX(i) = arrayU(j) Then
   bed_in_use = bed_in_use + 1

   Cells(i, "Y").Value = bed_in_use
    End If
    End If
Zapata
  • 133
  • 1
  • 5
  • 20
  • 2
    That would depend on your declaration of the arrays. Show the declaration lines. As in `Dim arrayx(...` – Scott Craner Apr 14 '16 at 23:28
  • Dim strDate Dim Count As Integer Dim arrayU() As Variant Dim arrayX() As Variant Dim arrayW() As Variant Dim LrowU As Integer Dim LrowX As Integer Dim LrowW As Integer Dim i As Integer Dim j As Integer Dim bed_in_use As Integer – Zapata Apr 14 '16 at 23:31
  • 1
    not in the comments please, put it in the original post. – Scott Craner Apr 14 '16 at 23:31
  • You need to set the size of the Array. Just before your loops you need to do this for all three. `ReDim ArrayX(3 to Lrowx) as Variant` do it for all three. – Scott Craner Apr 14 '16 at 23:39

1 Answers1

0

Check this page for information about this error.

In your case i'd say it is the first option :

  • You referenced a nonexistent array element.

Check if the values of i, r and j aren't larger than what the arrays where dimensioned for...


Either that or:

  • You declared an array but didn't specify the number of elements.
M.M
  • 2,254
  • 1
  • 20
  • 33