循环遍历表中的每一行

时间:2014-06-26 11:57:03

标签: vba excel-vba excel

我已将表格定义为卡片(参见图片:https://www.flickr.com/photos/113328996@N07/

我现在想要创建一些VBA代码,它执行以下操作:

  1. 遍历整个表格
  2. 计算它击中的时间' " A"
  3. 我想做这样的事情

    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
    

    但我无法找到使其正常工作的语法。有人能帮助我吗?

    亲爱的,

    马克

2 个答案:

答案 0 :(得分:3)

一种方式:

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

但使用Application.Countif会更简单!

答案 1 :(得分:2)

这可能过于冗长,但应该给你一个想法。

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