使用 VBA 循环遍历 Excel 中表格范围的第一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12744899/
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
Loop through the first column of a table range in Excel using VBA
提问by harryg
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.
我在工作表中有一个名为“ClientReturns”的表。此表的第一列包含客户的帐号。
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
采纳答案by brettdj
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
Next
回答by KekuSemau
For Each tRow In ClientTable
is interpreted as For Each tRow In ClientTable.Cells
Try this instead:For Each tRow In ClientTable.Rows
For Each tRow In ClientTable
被解释为For Each tRow In ClientTable.Cells
试试这个:For Each tRow In ClientTable.Rows