简体   繁体   English

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

[英]Iterate over all rows and columns in table with 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. 该函数只调用多个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. 我尝试使用ListRows和ListColumns的组合,但不知道在哪里去后手。 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. 任何帮助表示赞赏。

Dim Cell as Range

For Each Cell in listObj.DataBodyRange
...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM