Consulting

Results 1 to 9 of 9

Thread: Solved: Cancel a Loop if .Find Parameters Do Not Exist

  1. #1
    VBAX Contributor
    Joined
    Aug 2011
    Posts
    126
    Location

    Solved: Cancel a Loop if .Find Parameters Do Not Exist

    The Code Below Performs as intended, but it locks up when parameters identified in the .find command do not exist.

    How may I Exit Do if the Set c = .FindNext(c) is not found? [vba]
    With Sheets("sheet2").Range("a:a")
    Dim r As Range
    Set r = .Find(Sheets("sheet1").ComboBox1, lookat:=xlWhole)
    If r.Offset(0, 5) = "" Then
    r.Offset(0, 5) = Date
    r.Offset(0, 6) = Sheets("sheet1").ComboBox4
    r.Offset(0, 7).FormulaR1C1 = "=sum(rc[-2]-rc[-6])"
    Else

    Set c = .Find(Sheets("sheet1").ComboBox1, lookat:=xlWhole)
    If Not c.Offset(0, 5) Is Nothing Then
    Do
    Set c = .FindNext(c)
    If c.Offset(0, 5) Is Nothing Then
    Exit Do
    End If
    Loop Until c.Offset(0, 5) = ""
    End If
    c.Offset(0, 5) = Date
    c.Offset(0, 6) = Sheets("sheet1").ComboBox4
    c.Offset(0, 7).FormulaR1C1 = "=sum(rc[-2]-rc[-6])"
    End If
    End With

    [/vba]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Test c for Nothing, not its Offset cell.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Apparently you want to filter, so why not using a filter ?

    [vba]
    With Sheets("sheet2").usedrange.columns(1)
    .autofilter 1,.ComboBox1.value
    for each cl in .specialcells(12)
    if cl.Offset(0, 5) = "" Then cl.Offset(0, 5).resize(,3)=Array(Date, Sheets("sheet1").Oleobjects("ComboBox4").object.value, "=sum(rc[-2]-rc[-6])")
    .autofilter
    End With
    [/vba]


  4. #4
    VBAX Contributor
    Joined
    Aug 2011
    Posts
    126
    Location
    Thank You,

    Checking for C is Nothing (configured as shown) continues to lock up.

    [vba]Private Sub CommandButton1_Click()

    With Sheets("sheet2").Range("a:a")
    Dim r As Range
    Set r = .Find(Sheets("sheet1").ComboBox1, lookat:=xlWhole)
    If r.Offset(0, 5) = "" Then
    r.Offset(0, 5) = Date
    r.Offset(0, 6) = Sheets("sheet1").ComboBox4
    r.Offset(0, 7).FormulaR1C1 = "=sum(rc[-2]-rc[-6])"
    Else
    Set = .Find(Sheets("sheet1").ComboBox1, lookat:=xlWhole)
    If Not c.Offset(0, 5) Is Nothing Then
    Do
    Set c = .FindNext(c)
    If c Is Nothing Then ' HANGS UP IF c IS NOT FOUND IN RANGE "A:A"
    exit do
    GoTo line1
    End If
    If c.Offset(0, 5) Is Nothing Then
    Exit Do
    End If
    Loop Until c.Offset(0, 5) = ""
    End If
    c.Offset(0, 5) = Date
    c.Offset(0, 6) = Sheets("sheet1").ComboBox4
    c.Offset(0, 7).FormulaR1C1 = "=sum(rc[-2]-rc[-6])"
    End If
    End With
    Exit Sub

    line1:
    MsgBox "There Are No Valid Entries For This Query"



    End Sub[/vba]

    Filtering, Yes but No. The purpose is to find the next row with Combobox1.Value - Insert Data into Offset(0,5,) (if it is null)- Exit Sub.

    If there are no rows with null values in offset(0,5). GoTo Line1 should occurr. I am attaching a sample if you would review.

    Thanks.
    Attached Files Attached Files

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How can it be not found, you are selecting from lists that are based on the real data?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Contributor
    Joined
    Aug 2011
    Posts
    126
    Location
    I've asked myself that repeatedly.

    I generated a worksheet to try and figure out the issue. (uploaded)

    I inserted the:
    [vba]If c Is Nothing Then
    Exit Do
    Goto line1
    End If[/vba]
    As you Previously noted.

    I thought this would provide instruction to redirect commands should "c" be "Null" in range A:A. But... it doesn't in the current syntax.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What values did you select that generate the error?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Contributor
    Joined
    Aug 2011
    Posts
    126
    Location
    If a query is made to identify .Find(Sheets("sheet1").ComboBox1, lookat:=xlWhole) and the value of .offset(0,5) does not equal " "

    In this case, the condition that .FindNext(c) Is Nothing would be true.

    At this point the process hangs up although I thought I placed instruction to redirect the path in this instance.

  9. #9
    VBAX Contributor
    Joined
    Aug 2011
    Posts
    126
    Location
    Simple enough solution.... Just could not see it (as is usually the case)

    The code was in a vicious circle.

    Corrected it as shown below. :

    [vba]Private Sub CommandButton1_Click()
    With Sheets("sheet2").Range("a:a")
    Dim r As Range
    Set r = .Find(Sheets("sheet1").ComboBox1, LookAt:=xlWhole)
    If r.Offset(0, 5) = "" Then
    r.Offset(0, 5) = Date
    r.Offset(0, 6) = Sheets("sheet1").ComboBox4
    r.Offset(0, 7).FormulaR1C1 = "=sum(rc[-2]-rc[-6])"
    Else

    Set c = .Find(Sheets("sheet1").ComboBox1, LookAt:=xlWhole)

    If Not c.Offset(0, 5) Is Nothing Then
    firstaddress = c.Address

    Do
    Set c = .FindNext(c)
    Loop Until c.Offset(0, 5) = "" Or _
    c.Address = firstaddress
    End If

    c.Offset(0, 5) = Date
    c.Offset(0, 6) = Sheets("sheet1").ComboBox4
    c.Offset(0, 7).FormulaR1C1 = "=sum(rc[-2]-rc[-6])"
    End If
    End With
    End Sub[/vba]

    I did not have the c.address = firstaddress reference inserted.
    in addition to the Loop Until firstaddress=c.address so that it would exit.

    Thanks...
    This one is solved...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •