Share via

VBA: Do While executed when condition is false

Anonymous
2012-06-29T17:56:35+00:00

I'm getting the error "Subscript out of range" in some VBA code that shouldn't even be executing.  I have no idea how this is happening; I've tried quitting Excel and re-opening the spreadsheet, but the error keeps occurring.

As I'm sure you know, when I get the error and choose Debug in the dialog it takes you to the code and highlights where the error occurred.  In the code below, the error is occurring on line 35 at "WoodFinishes(w, f)".

When you hover over a variable, it also shows shows you its value when the error occurred.  I have checked all the variables, and found that:

f = 14

fMax = 13

So, because of line 32, line 35 should not even be executing.  Unless I'm just missing something that's completely obvious.  I've looked over this many times and can't find anything wrong.

Any ideas on how this is happening or how to fix it???

###############################################################################

(Sorry for what I'm sure will be awful formatting after I submit this, but I don't see any way to do code tags.)

Dim sheetUsedRange As Range         ' used range of sheet

Dim lastRow As Integer              ' number of last row in used range

Dim lastColumn As String            ' name of last column in used range

Dim WoodFinishes As Variant         ' range: start of Wood and Finish Combinations table to end of used range

Set sheetUsedRange = ActiveSheet.UsedRange

lastRow = sheetUsedRange.Rows.Count

lastColumn = get_colName(sheetUsedRange.Columns.Count)

WoodFinishes = Range("M2:" & lastColumn & lastRow).Value

' determine number of wood-finish combinations to know how wide the table is

Dim wStart As Integer               ' row number of first wood within WoodFinishes

Dim wOffset As Integer              ' offset from wStart to loop through woods

Dim w As Integer                    ' current row within WoodFinishes (wStart + wOffset)

Dim wMax As Integer                 ' maximum value of w (limited by UsedRange)

Dim fStart As Integer               ' column number of first finish within WoodFinishes

Dim fOffset As Integer              ' offset from fStart to loop through finishes

Dim f As Integer                    ' current column within WoodFinishes (fStart + fOffset)

Dim fMax As Integer                 ' maximum value of f (limited by UsedRange)

Dim numWoodFinishes As Integer      ' number of valid wood-finish combinations (defined in Wood and Finish Combinations)

wStart = 3

wOffset = 0

w = wStart + wOffset

wMax = lastRow - 1

fStart = 3

fOffset = 0

f = fStart + fOffset

fMax = sheetUsedRange.Columns.Count - get_colNum("M") + 1

numWoodFinishes = 0

' go across rows, down columns

Do While w <= wMax

    If IsEmpty(WoodFinishes(w, 1)) Then Exit Do

    Do While f <= fMax

        If IsEmpty(WoodFinishes(1, f)) Then Exit Do

        f = fStart + fOffset

        If WoodFinishes(w, f) <> "" Then numWoodFinishes = numWoodFinishes + 1

        fOffset = fOffset + 1

    Loop

    wOffset = wOffset + 1

    w = wStart + wOffset

    fOffset = 0

    f = fStart + fOffset

Loop

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2012-06-29T18:15:54+00:00

You have Do While f <= fMax, but within the loop you have

f = fStart + fOffset

After that line, f might well be > fMax.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2012-07-02T12:49:32+00:00

    Yes, a completely obvious mistake - that was supposed to be the last line in the loop.  Thanks so much!

    Was this answer helpful?

    0 comments No comments