Consulting

Results 1 to 6 of 6

Thread: Find cell, make active cell and repeat

  1. #1

    Find cell, make active cell and repeat

    Good Morning

    I have this code , it works fine except I need it to make the current founded cell with the "true" statement the Activecell(or Row) and run the call Macro on the active cell , and then move to the next "true" find and make that the Active and repeat the process.
    The macro needs to do its business on the active row of every "true" statement
    Problem is it finds the first "true" value , and stops

    Sub TRUEVALUE()
    Dim c As Range
    With Sheet1.Range("C1:C10")
     Set c = .Find("TRUE", LookIn:=xlValues)
        
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
           c.Select
                 Call TOTALVALUE
               
                Set c = .Find(c)
           Loop While Not c Is Nothing And c.Address <> firstAddress
        
        End If
    
    
     End With
     End Sub

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I think that changing this line will do what you want.
    Set c = .FindNext(After:=c)
    At the same time, I don't think that you need to actually select the found cells. TOTALVALUE could be altered to accept a range argument, so that no selecting is needed.

  3. #3
    Thank you , works perfectly, on the same sheet , having problem running macro that runs between sheets.
      Loop While Not c Is Nothing And c.Address <> firstAddress
    error 94 , Object variable or with block variable not set

  4. #4
    Thank you so much for your effort , works perfect in my Macro I had another search that search for a keyword and add pagebreak , that somehow interfered with this one

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Keep it simple

    sub M_snb()
      sn=Sheet1.Range("C1:C10")
      for j=1 to ubound(sn)
        if sn(j,1) then ...
      next
    End sub
    That's all you need. (and even less if you tell us what 'totalvalue' does).
    Allways avoid 'select' and 'activate' in VBA.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Can you change TOTALVALUE to accept a Range as an input?

    I don't like to rely on Selection or ActiveCell being what I think / hope they are




    Sub TRUEVALUE() 
        Dim c As Range 
        With Sheet1.Range("C1:C10") 
            Set c = .Find("TRUE", LookIn:=xlValues) 
             
            If Not c Is Nothing Then 
                firstAddress = c.Address 
                Do 
                    Call TOTALVALUE  (c)
                     
                    Set c = .FindNext(After:=c) 
                Loop While Not c Is Nothing And c.Address <> firstAddress 
                 
            End If 
             
             
        End With 
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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