首页 > 解决方案 > 将表格行设置为范围

问题描述

一旦我通过 for 循环识别了一个感兴趣的单元格,我如何才能从包含所述感兴趣的单元格的整个表格行中创建一个范围?我只需要我较大代码的一小部分的帮助。

'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.

我想要一个变量(即 - Trans_Queue_Row)来标识包含 TransCell 的整个表行。

标签: excelvbarowlistobject

解决方案


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

推荐阅读