Consulting

Results 1 to 6 of 6

Thread: Select multiple rows based on selected column

  1. #1
    VBAX Newbie
    Joined
    Apr 2010
    Posts
    3
    Location

    Solved - Select multiple rows based on selected column

    I have a spreadsheet that shows a grouping of data. Each column represents a message transaction. Each row represents a specific segment. A message can contain one or many segments. What I would like to do is that when an entire column is selected each row that has a value for that column to also be selected. This will highlight what segments are used in that particular message.

    ie.

    A B C D
    1 X X X
    2 X
    3 X X

    If Column B is selected Rows 1,2,3 would also be selected. If column D were selected row 1 would be selected. This would only happen if the entire column was selected. If an individual cell is selected then nothing else would be selected.

    Thanks
    Last edited by RcNorth; 04-30-2010 at 12:30 PM. Reason: Solved

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rng As Range
    Dim mCol As Range
    Dim LastRow As Long
    Dim i As Long

    On Error GoTo ws_exit
    Application.EnableEvents = False

    For Each mCol In Target.Columns

    LastRow = Me.Cells(Me.Rows.Count, mCol.Column).End(xlUp).Row
    For i = 1 To LastRow

    If Me.Cells(i, mCol.Column).Value2 <> "" Then

    If rng Is Nothing Then

    Set rng = Me.Cells(i, mCol.Column).EntireRow
    Else

    Set rng = Union(rng, Me.Cells(i, mCol.Column).EntireRow)
    End If
    End If
    Next i
    Next mCol

    If Not rng Is Nothing Then rng.Select
    Target.Cells(1, 1).Activate

    ws_exit:
    Application.EnableEvents = True

    End Sub
    [/vba]

    This is worksheet event code, which means that it needs to be
    placed in the appropriate worksheet code module, not a standard
    code module. To do this, right-click on the sheet tab, select
    the View Code option from the menu, and paste the code in.
    ____________________________________________
    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
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    The following is triggered by a Selection Change and needs to be place in the code module for the appropriate sheet.

    [vba]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rng As Range, cCell As Range
    Dim lRow As Long
    Application.EnableEvents = False
    If Target.Rows.Count = Rows.Count Then
    lRow = Cells(Rows.Count, Target.Column).End(xlUp).Row

    For Each cCell In Target
    If cCell.Row > lRow Or lRow = 1 Then Exit For
    If cCell.Row = 1 Then
    Else
    If cCell <> "" Then
    If rng Is Nothing Then
    Set rng = cCell.EntireRow
    Else
    Set rng = Union(rng, cCell.EntireRow)
    End If
    End If
    End If
    Next

    End If

    If Not rng Is Nothing Then
    rng.Select
    End If
    Application.EnableEvents = True
    End Sub[/vba]
    Last edited by mbarron; 04-30-2010 at 12:20 PM. Reason: I see I am 20 minutes late... XLD has already posted a solution.

  4. #4
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    This one will keep the Column highlighted as well
    [VBA]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rng As Range, cCell As Range
    Dim lRow As Long

    If Target.Rows.Count = Rows.Count Then
    lRow = Cells(Rows.Count, Target.Column).End(xlUp).Row

    For Each cCell In Target
    If cCell.Row > lRow Or lRow = 1 Then Exit For
    If cCell.Row = 1 Then
    Else
    If cCell <> "" Then
    If rng Is Nothing Then
    Set rng = cCell.EntireRow
    Else
    Set rng = Union(rng, cCell.EntireRow)
    End If
    End If
    End If
    Next

    End If

    If Not rng Is Nothing Then
    Set rng = Union(rng, Target)
    rng.Select
    End If
    End Sub
    [/VBA]

  5. #5
    VBAX Newbie
    Joined
    Apr 2010
    Posts
    3
    Location
    Thanks guys the both come very close to the needed solution. mbarron is a bit better as it allows the selection of a single cell or row without selecting everything. Where as xld's solution will highlight the spreadsheet whenever a cell that has a value is selected.

    One thing that I need to figure out to add to mbarron's solution is how to select the chosen column as well as all the rows. Currently the selected column is unselected so you don't know which message the selected segments are for.

  6. #6
    VBAX Newbie
    Joined
    Apr 2010
    Posts
    3
    Location
    Thanks mbarron the last one is exactly what I am looking for. I guess you were replying while I was typing up my last comment.

    Consider this closed.

Posting Permissions

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