Home » excel » excel – Setting a table row as a range

excel – Setting a table row as a range

Posted by: admin May 14, 2020 Leave a comment


Once I have identified a cell of interest via a for loop, how can I then make a range out of the entire table row that contains said cell of interest?
I need help with only this small part of my larger code.

'TransColumn is a table column in which I am looking for the phrase "NPD".
'TransCell is my cell of interest, containing the phrase "NPD".

'I want Trans_Queue_Row to be the table row in which TransCell is located.  

                        For Each TransCell In TransColumn
                        If InStr(1, TransCell.Value, "NPD") > 0 Then

                        Dim Trans_Queue_Row As Range
                        Set Trans_Queue_Row = ThisWorkbook.Sheets("Project Queue").ListObjects("TableQueue").ListRows

'I know this looks like a weird way to achieve what I'm asking for, but I'm using InStr to support some other elements of my code not displayed here.

I want a variable (i.e. – Trans_Queue_Row) to identify the entire table row that contains TransCell.

How to&Answers:
Dim TableQueue as ListObject, Trans_Queue_Row As Range, i as Long
Set TableQueue = ThisWorkbook.Sheets("Project Queue").ListObjects("TableQueue")

With TransColumn.DataBodyRange
    For i = 1 To .Count
        If InStr(1, .Rows(i).Value, "NPD") > 0 Then 
            Set Trans_Queue_Row = TableQueue.DataBodyRange.Rows(i)
        End If
    Next i
End With


From my answer to your previous question:

Trans_new_NPD_row.Range.Value = _
     Application.Intersect(TransCell.EntireRow, QueueTable.DataBodyRange).Value

You can use Intersect to find the range common to TransCell.EntireRow and the data section of the table/listobject.