Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15537

Opinions On How To Deal With This "Subscript Out Of Range" Error

$
0
0
Hello,

I'm looking for opinions on the best way to deal with the "Subscript out of range" Error
generated by the following procedure.

I'm sure I can fix it a number of ways, but I have approximately one hundred of similar procedures,
and I want to only fix it ONCE, so I want to make sure I do it the RIGHT way.

I had tested this (and the hundred others) using test files that would build arrays with TWO or more elements.
Everything worked as it should.

When I realized I didn't test for arrays with ONE element, and tested for that - CRASH! (@ red line below)

Any help offered would be appreciated.

Also, am I correct in my understanding, that the If block as I have it constructed below, will execute
ONLY THE FIRST If or ElseIf that evaluates to TRUE?

Thank you in advance.

Code:

Public Sub RepositionForPreNoteTimeSigs(BarNum As Long)
   
    Dim t As Long  'iterator for track
    Dim o As Long  'iterator for object

    Dim widestTimeSig As Long
    widestTimeSig = GetWidestTimeSig()

    ' Reposition objects as required
    For t = 1 To gTracksPerSystem
        If (RTrim(BarObjs(t).Obj(1).ObjectType) = "TimeSig") Then
            ' TimeSig @ pos 1
            For o = 2 To UBound(BarObjs(t).Obj)
                BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                            widestTimeSig - _
                                SpaceAfterObject(BarObjs(t).Obj(1).glyphNum)
            Next o
        ElseIf (RTrim(BarObjs(t).Obj(2).ObjectType) = "TimeSig") Then
            ' TimeSig @ pos 2
            For o = 3 To UBound(BarObjs(t).Obj)
                BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                            widestTimeSig - _
                                SpaceAfterObject(BarObjs(t).Obj(2).glyphNum)
            Next o
        ElseIf (RTrim(BarObjs(t).Obj(3).ObjectType) = "TimeSig") Then
            ' TimeSig @ pos 3
            For o = 4 To UBound(BarObjs(t).Obj)
                BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                            widestTimeSig - _
                                SpaceAfterObject(BarObjs(t).Obj(3).glyphNum)
            Next o
        ElseIf RTrim(BarObjs(t).Obj(1).ObjectType) = "Note" Or _
                    RTrim(BarObjs(t).Obj(1).ObjectType) = "Rest" Then
            ' No TimeSig, Note/Rest @ Pos 1
            For o = 1 To UBound(BarObjs(t).Obj)
                BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                            widestTimeSig
            Next o
        ElseIf (RTrim(BarObjs(t).Obj(2).ObjectType) = "Note") Or _
                    (RTrim(BarObjs(t).Obj(2).ObjectType) = "Rest") Then
            ' No TimeSig, Note/Rest @ pos 2
            For o = 2 To UBound(BarObjs(t).Obj)
                BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                            widestTimeSig
            Next o
        ElseIf (RTrim(BarObjs(t).Obj(3).ObjectType) = "Note") Or _
                    (RTrim(BarObjs(t).Obj(3).ObjectType) = "Rest") Then
            ' No TimeSig, Note/Rest @ pos 3
            For o = 3 To UBound(BarObjs(t).Obj)
                BarObjs(t).Obj(o).ColCurr = BarObjs(t).Obj(o).ColCurr + _
                            widestTimeSig
            Next o
        End If
    Next t

End Sub


Viewing all articles
Browse latest Browse all 15537

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>