VBA code to see if an entire row has been selected.

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I need some code to see if an entire row has been selected within a range and also I need code to see if an entire row has been selected within a table, tblCosting.

Thanks,
Dave
 
igold's code doesn't comprehend the possibility that multiple, perhaps non-contiguous, table rows are selected - perhaps that's the source of your unexpected results.
This code will catch multiple row selections within the table including non-contiguous:
Code:
Sub test()
    Dim tbl As Object, Ar As Range
    Dim i As Long, j As Long, k As Long
    Set tbl = ActiveSheet.ListObjects("tblCosting")
    If TypeName(Selection) = "Range" Then
        If Not Intersect(Selection, tbl.Range) Is Nothing Then
            For i = 1 To Selection.Areas.Count
                For j = 1 To Selection.Areas(i).Rows.Count
                    For k = 1 To tbl.ListRows.Count
                        If Selection.Areas(i).Rows(j).Address = tbl.ListRows(k).Range.Address Then
                            MsgBox "Row: " & k & " of the table is selected."
                        End If
                    Next k
                Next j
            Next i
        Else
            MsgBox "No table rows selected"
        End If
    Else
        MsgBox "Current selection is not a range object"
    End If
End Sub

It displays the message of No table rows selected but if you have nothing selected but with a row selected, pressing the button will not display the other message box.
 
Last edited:
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I am creating a spreadsheet for work and I want to add in a check for when quotes don't get accepted and need to be transferred to a different section of the another workbook as a reminder to the user to select the row first.
 
Upvote 0
Maybe this....but my question remains.....WHY ???

Code:
Sub MM1()
ActiveCell.EntireRow.Select
If Intersect(ActiveCell.EntireRow, ActiveSheet.ListObjects("tblCosting").DataBodyRange) Is Nothing Then
      MsgBox "active row is NOT within the table"
   Else
      MsgBox "active row is within the table"
End If
End Sub

That is perfect thanks Michael :)
 
Upvote 0
I took that code and tried to add a procedure into the else part, if the row is selected but it won't work. The procedure is otherwise working. Could you have a look at my code please to see what I have left out.

Code:
Sub cmdNot_Accept_Click()
Dim wsDst As Worksheet
Dim wsSrc As Worksheet
Dim tblrow As ListRow
Dim Combo As String
Dim sht As Worksheet
Dim tbl As ListObject
Dim LastRow As Long
Dim DocYearName As String
        
ActiveCell.EntireRow.Select
With Selection
    If Intersect(ActiveCell.EntireRow, ActiveSheet.ListObjects("tblCosting").DataBodyRange) Is Nothing Then
          MsgBox "active row is NOT within the table"
       Else
          
          
           Combo = "Not accepted quotes"
                'lastrow = Worksheets(Combo).Cells(Rows.Count, "A").End(xlUp).Row + 1                                    'number of first empty row in column A of Combo
                    
                If tblrow.Range.Cells(1, 6).Value = "Ang Wes" Then
                    DocYearName = tblrow.Range.Cells(1, 37).Value
                Else
                    DocYearName = tblrow.Range.Cells(1, 36).Value
                End If
                Set wsDst = Workbooks(DocYearName).Worksheets(Combo)
                    With wsDst
                        'This copies the first 10 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.
                        tblrow.Range.Resize(, 8).Copy
                        .Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
    
                            Rows("3:1000").Select
                            Workbooks(DocYearName).Worksheets(Combo).Sort.SortFields.Clear
                            Workbooks(DocYearName).Worksheets(Combo).Sort.SortFields.Add Key:=Range("A4:A1000"), _
                                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                                    With Workbooks(DocYearName).Worksheets(Combo).Sort
                                        .SetRange Range("A3:AJ1000")
                                        .header = xlYes
                                        .MatchCase = False
                                        .Orientation = xlTopToBottom
                                        .SortMethod = xlPinYin
                                        .Apply
                                    End With
                    End With
          
          
          
    End If
End With
End Sub

Thanks Michael,
Dave
 
Upvote 0
Actually this is the entire code that relates to every row. What do I change to make it apply to only the selected row?

Code:
        For Each tblrow In tbl.ListRows
            Combo = tblrow.Range.Cells(1, 26).Value
            'lastrow = Worksheets(Combo).Cells(Rows.Count, "A").End(xlUp).Row + 1                                    'number of first empty row in column A of Combo
                
            If tblrow.Range.Cells(1, 6).Value = "Ang Wes" Then
                DocYearName = tblrow.Range.Cells(1, 37).Value
            Else
                DocYearName = tblrow.Range.Cells(1, 36).Value
            End If
            Set wsDst = Workbooks(DocYearName).Worksheets(Combo)
                With wsDst
                    'This copies the first 10 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.
                    tblrow.Range.Resize(, 8).Copy
                    .Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
                    '.Cells.Range(1, 9).PasteSpecial xlPasteFormulas

'.Range("A" & Rows.Count).End(xlUp).Offset(8).PasteSpecial xlPasteFormulas
                    
                    'This should go to the 15th column in the current row, i.e. column O, and copy that column and the next 2 columns, i.e. O:Q, to column K on the destination sheet.
                        'tblrow.Range.Offset(, 14).Resize(, 3).copy
                        '.Range("K" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
                    'Similarly this should copy columns AD:AF from the table to column N on the destination sheet.
                        'tblrow.Range.Offset(, 29).Resize(, 3).copy
                        '.Range("N" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
                    'Sort rows based on date
                        Rows("3:1000").Select
                        Workbooks(DocYearName).Worksheets(Combo).Sort.SortFields.Clear
                        Workbooks(DocYearName).Worksheets(Combo).Sort.SortFields.Add Key:=Range("A4:A1000"), _
                            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                                With Workbooks(DocYearName).Worksheets(Combo).Sort
                                    .SetRange Range("A3:AJ1000")
                                    .header = xlYes
                                    .MatchCase = False
                                    .Orientation = xlTopToBottom
                                    .SortMethod = xlPinYin
                                    .Apply
                                End With
                End With
        Next tblrow


Thanks Michael,
Dave
 
Upvote 0
I don't have Excel at the moment, but i notice you have declared
Dim tblrow As ListRow

But ListRow of what ??
 
Upvote 0
I just copied the procedure that I know worked, not sure if I actually needed it all or not.
 
Upvote 0
Cant test it but why not

Code:
Sub cmdNot_Accept_Click()
Dim wsDst As Worksheet, wsSrc As Worksheet
Dim tblrow As ListRow, Combo As String
Dim sht As Worksheet, tbl As ListObject
Dim LastRow As Long, DocYearName As String
        
[color=red]ActiveCell.EntireRow.Select
With Selection
    If Intersect(ActiveCell.EntireRow, ActiveSheet.ListObjects("tblCosting").DataBodyRange) Is Nothing Then
          MsgBox "active row is NOT within the table"
       Exit Sub
       End If[/color]
          
          
           Combo = "Not accepted quotes"
                'lastrow = Worksheets(Combo).Cells(Rows.Count, "A").End(xlUp).Row + 1                                    'number of first empty row in column A of Combo
                    
                If tblrow.Range.Cells(1, 6).Value = "Ang Wes" Then
                    DocYearName = tblrow.Range.Cells(1, 37).Value
                Else
                    DocYearName = tblrow.Range.Cells(1, 36).Value
                End If
                Set wsDst = Workbooks(DocYearName).Worksheets(Combo)
                    With wsDst
                        'This copies the first 10 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.
                        tblrow.Range.Resize(, 8).Copy
                        .Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
    
                            Rows("3:1000").Select
                            Workbooks(DocYearName).Worksheets(Combo).Sort.SortFields.Clear
                            Workbooks(DocYearName).Worksheets(Combo).Sort.SortFields.Add Key:=Range("A4:A1000"), _
                                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                                    With Workbooks(DocYearName).Worksheets(Combo).Sort
                                        .SetRange Range("A3:AJ1000")
                                        .header = xlYes
                                        .MatchCase = False
                                        .Orientation = xlTopToBottom
                                        .SortMethod = xlPinYin
                                        .Apply
                                    End With
                    End With
          
          
          
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top