Share via

need help with a run time error 91 debug

Anonymous
2016-10-18T01:07:56+00:00

Hello,

I am having trouble with a form I'm using. I need a little help with a (probably really simple) debug.

I think I know what is causing the problem, but don't know what to do to fix it. Perhaps an If statement that would perform Next rngNext?

Whenever the string "NA" is input to the form this will cause an issue and I get the "variable not defined or with block not defined" error. Any ideas?

Should I post my code? Should I post a link to the program? What works best?

Thanks for your help!!

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

4 answers

Sort by: Most helpful
  1. Anonymous
    2016-10-19T02:00:00+00:00

    Re:  more loops

    Step thru the following as is, then try it with "Exit For" commented out and GoTo FastExit line uncommented.

    Sub testingloopExample()

      On Error GoTo Squelch

      Dim N As Long

      For N = 1 To 10

       On Error Resume Next

       Err.Raise (91)

       If Err.Number <> 0 Then

          On Error GoTo Squelch ' clears error

         Exit For             ' This gets you completely out of the 10 loops.

    '     GoTo FastExit ' This puts you in the next loop

       Else

         On Error GoTo Squelch

        'do stuff

       End If

    FastExit:

      Next 'N

    'other stuff here

    Squelch:

    End Sub


    Jim Cone

    Portland, Oregon USA

    https://goo.gl/IUQUN2 (Dropbox)

    (free & commercial excel add-ins & workbooks)

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-10-19T00:49:02+00:00

    So youre saying that in my if statement i can say GoTo Fastexit? Like this?

            If IsError(resModel) Or IsError(resSize) Then

                  GoTo FastExit

           ElseIf IsError(resBrand) Or IsError(resInsul) Then

                  GoTo FastExit

           End If

           FastExit = Next rngNext

           Else.....

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-10-18T03:43:26+00:00

    Re: loops

    You can nest loops, but each individual loop inside (starting with the intermost) must be exited before the next one can continue.  You can exit a loop early by using a line label just ahead of "Next" or "Loop"...

    For N = Start to Finish

    'do stuff

    If something bad then GoTo FastExit

    'do more stuff

    FastExit:

    Next

    '---

    Jim Cone

    Portland, Oregon USA

    https://goo.gl/IUQUN2 (Dropbox)

    (free & commercial excel add-ins & workbooks)

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2016-10-18T02:57:49+00:00

    Here is the code for the form...some comments are to describe what I was doing and some are for things that I want to add or delete.

    Option Explicit

    Dim Saved As Boolean

    Dim ThisRow As Range

    Dim rngInfo As Range

    Dim rngNext As Range

    Private Sub Cleardata()

      'Remove all data from the controls

      Dim C As MSForms.Control

      For Each C In Me.Controls

        If C.Tag <> "" Then C = ""

      Next

      Saved = True

    End Sub

    Private Sub LoadData()

      'Load the data from current row into the controls

      Dim C As MSForms.Control

      For Each C In Me.Controls

        If C.Tag <> "" Then

          C = Range(C.Tag & ThisRow.Row)

        End If

      Next

      Saved = True

    End Sub

    Private Sub ModelBox_Change()

        Saved = True

    End Sub

    Private Sub QuantityBox_Change()

        Saved = True

    End Sub

    Private Sub SizeBox_Change()

        Saved = True

    End Sub

    Private Sub CommandButton2_Click()

    Unload Me

    '*    Application.Goto Worksheets("INFO").Cells(2, "A"), Scroll:=True -> will delete

    End Sub

    Private Sub CommandButton3_Click()

        Dim rModel As Range, rSize As Range, rModel1 As Range, rSize1 As Range

            Dim resModel As Variant, resSize As Variant

            Dim sModel As String, sSize As String, Quantity As Long

            Dim Lsize As Variant

            Dim Length As Range

        Dim rBrand As Range, rInsul As Range, rBrand1 As Range, rInsul1 As Range

            Dim resBrand As Variant, resInsul As Variant

            Dim sBrand As String, sInsul As String

            Dim LInsul As Variant

            Dim Kind As Range

         Dim emptyColumn, i As Integer

         Set Length = Nothing

         Set Kind = Nothing

        With Worksheets("INFO")

            Select Case Me.CommandButton3.Caption

                Case "Get Info"

                    'Set variable to entire range from A2 to bottom of data in column A

                    Set rngInfo = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))

                Case "Next Info"

                    If rngNext Is Nothing Then

                        Me.CommandButton3.Caption = "Get Info"

                        MsgBox "No more data"

                        Exit Sub

                    End If

                    If rngNext.Address = .Cells(.Rows.Count, "A").End(xlUp).Address Then

                        Me.CommandButton3.Caption = "Get Info"

                        MsgBox "No more data"

                        Exit Sub

                    Else

                        'Set rngInfo variable to the next cell past last found to bottom of data

                        Set rngInfo = .Range(rngNext.Offset(1, 0), .Cells(.Rows.Count, "A").End(xlUp))

                    End If

            End Select

        End With

        For Each rngNext In rngInfo

    '*             Do  <- Need this to go with Loop until rngNext = 0  -> want to use this, don't know where to place it correctly

            If rngNext.Value > 0 Then

                Me.QuantityBox.Value = rngNext.Value

                If Not IsError(rngNext.Offset(0, 1).Value) Then

                    Me.ModelBox = rngNext.Offset(0, 1).Value

                Else

                    Me.ModelBox = "Error"

                End If

                If Not IsError(rngNext.Offset(0, 2).Value) Then

                    Me.SizeBox = rngNext.Offset(0, 2).Value

                Else

                    Me.SizeBox = "Error"

                End If

                If Not IsError(rngNext.Offset(0, 3).Value) Then

                    Me.InsulationBox = rngNext.Offset(0, 3).Value

                Else

                    Me.InsulationBox = "Error"

                End If

        With Worksheets("Sheet3")

           Set rModel = .Range("C5", .Range("C5").End(xlToRight))

           Set rSize = .Range("B7", .Range("B7").End(xlDown))

        End With

         sModel = rngNext.Offset(0, 1).Value

         sSize = rngNext.Offset(0, 2).Value

            Select Case ModelBox.Value

            Case "TFS", "ESV", "TQP", "TQS", "MDV", "LHK", "LSC", "EXV", "FLS", "EDV"

                With Worksheets("Sheet2")

                    Set rBrand = .Range("B3")

                    Set rInsul = .Range("A4", .Range("A4").End(xlDown))

                End With

            Case "LMHS", "QFC", "QFV", "KQFS", "KQFP", "KLPS", "KLPP", "LMHD", "LMHDT"

                With Worksheets("Sheet2")

                    Set rBrand = .Range("C3")

                    Set rInsul = .Range("A4", .Range("A4").End(xlDown))

                End With

            Case "SDV", "RRV", "DSV", "DDV", "DDC", "FPC", "IDV", "FPV"

                With Worksheets("Sheet2")

                    Set rBrand = .Range("E3")

                    Set rInsul = .Range("A4", .Range("A4").End(xlDown))

                End With

            Case "35E", "35L", "35M", "35N", "42K", "45J", "45K", "45M", "45N", "45Q", "45R"

                With Worksheets("Sheet2")

                    Set rBrand = .Range("D3")

                    Set rInsul = .Range("A4", .Range("A4").End(xlDown))

                End With

        End Select

         sBrand = rBrand.Value

         sInsul = rngNext.Offset(0, 3).Value

            Me.CommandButton3.Caption = "Next Info"

         If IsNumeric(sSize) Then

            Lsize = CLng(sSize)

         Else

            Lsize = sSize

         End If

         If IsNumeric(sInsul) Then

            LInsul = CLng(sInsul)

         Else

            LInsul = sInsul

         End If

         Quantity = CLng(QuantityBox.Value)

         resModel = Application.Match(sModel, rModel, 0)

         resSize = Application.Match(Lsize, rSize, 0)

         resBrand = Application.Match(sBrand, rBrand, 0)

         resInsul = Application.Match(LInsul, rInsul, 0)

               If IsError(resModel) Or IsError(resSize) Then

           MsgBox "Not Found"

    '*       Next rngNext  -> don't think this will work, would like it to move on to next line if error

               ElseIf IsError(resBrand) Or IsError(resInsul) Then

           MsgBox "Not Found"

    '*       Next rngNext  -> don't think this will work, would like it to move on to next line if error

         Else

           Debug.Print resModel, resSize

           Debug.Print resBrand, resInsul

           Set rModel1 = rModel(resModel)

           Set rSize1 = rSize(resSize)

           Set Length = Intersect(rModel1.EntireColumn, rSize1.EntireRow)

           Set rBrand1 = rBrand(resBrand)

           Set rInsul1 = rInsul(resInsul)

           Set Kind = Intersect(rBrand1.EntireColumn, rInsul1.EntireRow)

         End If

        Worksheets("Sheet1").Activate

        i = 1

        Do

            emptyColumn = WorksheetFunction.CountA(Range("A:ZZ")) + i

            Cells(i, emptyColumn).Value = Quantity * Length.Value

            Cells(i + 1, emptyColumn).Value = Kind.Value

            Cells(i + 2, emptyColumn).Value = QuantityBox.Value & " " & ModelBox.Value

            i = i + 1

        Loop Until i = 2

                Exit For

    '*    Loop Until rngNext = 0  -> would like to use this, don't know where to put it correctly

            End If

        Next rngNext

    End Sub

    Private Sub UserForm_Initialize()

        Me.CommandButton3.Caption = "Get Info"

    End Sub

    Was this answer helpful?

    0 comments No comments