Consulting

Results 1 to 8 of 8

Thread: Select multiple rows based on cell value

  1. #1

    Select multiple rows based on cell value

    I am using the following code to select the entire row where cell value is equal to "Total"
    But this code is not selecting the entire row of all the cells containing the value "Total" and only selects the Last cell row.

    This is what I have observed that the code releases the lase selection when new row is selected.

    How can I fix the problem?

    code is:
    Sub SelRows()
    Dim ocell As Range
    For Each ocell In Range("B1:B10")
    If ocell.Value = "Total" Then
    ocell.EntireRow.Select
    End If
    Next
    End Sub

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sub SelRows()
    Dim ocell As Range
    Dim rng As Range
    For Each ocell In Range("B1:B10")
    If ocell.Value = "Total" Then
    If rng Is Nothing Then
    Set rng = oCcell.EntireRow
    Else
    Set rng = Union(rng, ocell.EntireRow)
    End If
    End If
    Next
    If Not rng Is Nothing Then rng.Select
    Set rng = Nothing
    Set ocell = Nothing
    End Sub
    ____________________________________________
    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
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sub SelRows()
    Dim ocell As Range
    Dim rng As Range
    For Each ocell In Range("B1:B10")
    If ocell.Value = "Total" Then
    If rng Is Nothing Then
    Set rng = ocell.EntireRow
    Else
    Set rng = Union(rng, ocell.EntireRow)
    End If
    End If
    Next
    If Not rng Is Nothing Then rng.Select
    Set rng = Nothing
    Set ocell = Nothing
    End Sub
    Last edited by mdmackillop; 07-09-2009 at 02:20 AM. Reason: Typo corrected
    ____________________________________________
    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

  4. #4
    Thanks xld, its working
    One typo is there:
    Set rng = oCcell.EntireRow

    should be:
    Set rng = ocell.EntireRow

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, just rattled it off.
    ____________________________________________
    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 Regular
    Joined
    Feb 2019
    Posts
    9
    Location
    Hi

    Thank you, this code help to me also

    But I can not solve If I just want to select not EntireRow but only for example to I column

    Regards

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Welcome

    Please take a minute to review the links in my signatire to the FAQs

    However , this was a 10 year old post. It's better if you started a new one with a complete and stand alone description of your question

    Use the[+Post New Thread] button top left to start a new one
    ---------------------------------------------------------------------------------------------------------------------

    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

  8. #8
    VBAX Regular
    Joined
    Feb 2019
    Posts
    9
    Location
    Hello Paul

    And Thank you for your reply

    Regards
    Dan

Posting Permissions

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