循环以基于单元格值通过多个工作表删除表行

时间:2015-04-29 23:45:57

标签: excel vba loops row listobject

处理循环以从工作簿中的众多工作表中删除股票信息。每个工作表都命名为“Client_ClientFirstName”,每个工作表中的每个表都与工作表名称相同。这是我到目前为止提出的代码,任何和所有的建议表示赞赏。

Sub RemoveTickerFromAccounts()

Dim Client As Worksheet
Dim varTickerToFind As String
varTickerToFind = Worksheets("Entry").Cells(5, 1)


Dim tblSearchTable As Range

For Each Client In ActiveWorkbook.Worksheets

        If InStr(1, Client.Name, "Client_", vbTextCompare) Then
            'ws.Range("B30").Select
            Worksheets(Client.Name).Activate
            'tblSearchTable = ActiveSheet.ListObjects(1)

            ActiveSheet.Range("b30").Select
            If Selection.ListObject.Name = Client.Name Then
            'tblSearchTable = "Table14"


            'tblSearchTable = ActiveSheet.ListObjects(Client.Name).Select

        For i = 1 To ActiveSheet.ListObjects(Client.Name).ListRows.Count
            If ActiveSheet.ListObject.ListRows(i, 1).Value = varTickerToFind _
                Then
                tblSearchTable.ListRows(i).Delete

                Exit For
            Else
                MsgBox "Unable to Find Ticker"
                Exit For
            End If

        Next i
        End If
    End If
 Next Client


End Sub

1 个答案:

答案 0 :(得分:0)

测试:

Sub RemoveTickerFromAccounts()

Dim Client As Worksheet
Dim varTickerToFind As String
Dim tblSearch As ListObject
Dim bFound As Boolean, i As Long

    varTickerToFind = Worksheets("Entry").Cells(5, 1).Value

    For Each Client In ActiveWorkbook.Worksheets

        If Client.Name Like "Client_*" Then

            Set tblSearch = Client.ListObjects(1)

            For i = 1 To tblSearch.ListRows.Count
                If tblSearch.ListRows(i).Range.Cells(1).Value = varTickerToFind Then
                    tblSearch.ListRows(i).Delete
                    bFound = True
                    Exit For
                End If
            Next i

            If Not bFound Then 
                MsgBox "Unable to Find Ticker '" & varTickerToFind & "'"
            End If
        End If

     Next Client
End Sub