使用VBA迭代表中的所有行和列

时间:2021-10-27 14:31:42

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.

我在工作表中有一个表,我想迭代并更改使用我设置的函数的值。该函数只调用多个Replace函数来更改字符串。

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:

但是,我如何修改该代码以使其迭代遍历行?我尝试使用ListRows和ListColumns的组合,但不知道在哪里去后手。在我不确定之前,这是我到达的地方:

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.

任何帮助表示赞赏。

2 个解决方案

#1


2  

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

#2


4  

Dim Cell as Range

For Each Cell in listObj.DataBodyRange
...

#1


2  

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

#2


4  

Dim Cell as Range

For Each Cell in listObj.DataBodyRange
...