Home » excel » Loop through the first column of a table range in Excel using VBA

Loop through the first column of a table range in Excel using VBA

Posted by: admin March 9, 2020 Leave a comment


This should be pretty straightforward but I’m a little stuck.

I have a table called “ClientReturns” in a worksheet. The first column of this table contains the account number of a client.

I want to loop through the table getting the account number each time. This is what I have so far but it also goes through the data in the other columns when I only want the data in the first column.

Sub doStuff()

    Set ClientTable = Sheets("Returns").range("ClientReturns")
    For Each tRow In ClientTable
        AccNum = tRow.Columns(1).Value
        'Do stuff with the account num
    Next AccNum

End Sub

Many thanks for the answers… I solved it with this:

Sub getReturns()

    Dim lookR As range, c As range
    With Sheets("Returns").ListObjects("ClientReturns")
        Set lookR = .ListColumns("Account Number").DataBodyRange
        For Each AccNum In lookR.Cells
            'Let's see if it's a combined account
        Next AccNum
    End With

End Sub
How to&Answers:

More directly just work with the first column

Dim ClientTable As Range
Dim rng1 As Range
Set ClientTable = Sheets("Returns").Range("ClientReturns")
For Each rng1 In ClientTable.Columns(1).Cells
    AccNum = rng1.Value


For Each tRow In ClientTable
is interpreted as For Each tRow In ClientTable.Cells
Try this instead:
For Each tRow In ClientTable.Rows