Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over all rows and columns in table with VBA

Tags:

excel

vba

I have a table in a worksheet that I want to iterate over and change the value of using a function I had set up. The function just calls on multiple Replace functions to change the strings.

My question is how do I iterate over the values in the table only?

I was able to do it with columns using the following code:

For Each cell In Sheets("RawData").ListObjects("RawTable").ListColumns("REQUESTNAME").DataBodyRange.Cells
    cell.Value = decodeEnt(cell.Value)
Next

But how do I modify that code to make it iterate through the rows as well? I tried using a combination of ListRows and ListColumns, but didn't know where to go afterwads. Here is where I got to before I was unsure:

Dim listObj As ListObject
Set listObj = Sheets("RawData").ListObjects("RawTable")

For Each tableRow In listObj.ListRows
    For Each tableCol In listObj.ListColumns
        ' Would using intersect work here?
        ' listObj.Cell.Value = decodeEnt(cell.Value)
Next

Any help is appreciated.

like image 841
Mo2 Avatar asked Jan 21 '14 17:01

Mo2


1 Answers

I think you can't use a variant iteration like that, you have to use indexed iteration. Something like this (untested):

Dim listObj As ListObject, r%, c%
Set listObj = Sheets("RawData").ListObjects("RawTable")

For c = 1 To listObj.ListColumns.Count
    For r = 1 To listObj.ListRows.Count
        listObj.DataBodyRange.Cells(r, c).Value = decodeEnt(listObj.DataBodyRange.Cells(r, c).Value)
    Next
Next
like image 136
David Zemens Avatar answered Sep 19 '22 15:09

David Zemens