vba 遍历表中的每一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24429902/
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 each row in a table
提问by user3706202
I've defined a table as cards (see picture here: https://www.flickr.com/photos/113328996@N07/)
我已将表格定义为卡片(参见此处的图片:https: //www.flickr.com/photos/113328996@N07/)
I now want a create some VBA code which does the following:
我现在想要创建一些执行以下操作的 VBA 代码:
- Loop through the whole table
- Count the times it 'hits' "A".
- 遍历整个表
- 计算它“击中”“A”的次数。
I wantto do something like this
我想做这样的事情
sub countCards
//Open sheets: "sheet1"
dim countA as integer
countA = 0
//foreach each row in table "cards"
if (cells is "A")
countA = countA + 1
end if
But I can't find the syntax to get this working. Can anybody help me?
但我找不到让这个工作的语法。有谁能够帮助我?
Dear regards,
亲爱的问候,
Marc
马克
回答by Rory
One way:
单程:
Dim oList As ListObject
Dim oRow As ListRow
Dim counta As Long
Set oList = ActiveSheet.ListObjects("cards")
For Each oRow In oList.ListRows
If oRow.Range(1) = "A" Then counta = counta + 1
Next oRow
MsgBox counta
but using Application.Countif would be simpler!
但是使用 Application.Countif 会更简单!
回答by Hambone
This may be excessively verbose, but it should give you an idea.
这可能过于冗长,但它应该给你一个想法。
Sub countCards()
Dim table As ListObject
Dim tableData, rowData As range
Dim countA As Integer
Set table = Worksheets("Sheet1").ListObjects("Table1")
Set tableData = table.range
countA = 0
For Each rowData In tableData.Rows
If Cells(rowData.row, rowData.Column).Value = "A" Then
countA = countA + 1
End If
Next
End Sub