使用 VBA 迭代表中的所有行和列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21265148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over all rows and columns in table with VBA
提问by Mo2
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:
但是如何修改该代码以使其也遍历行?我尝试使用 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.
任何帮助表示赞赏。
采纳答案by David Zemens
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
回答by Excel Developers
Dim Cell as Range
For Each Cell in listObj.DataBodyRange
...