Count table rows with specific value in a column

K

kaz

Hi there

I have a table where I am counting the number of rows, excluding the
header row, with the result of the count being displayed in another
part of the Word document. That works very well using a macro (after I
sought advice here a while back).

Now I am told I need to have two counts displaying information from
this table. The first is the one I have already got. The second is a
count of rows in the table, excluding the header row, where one of the
columns has a value of A or M. Any clues on how I can achieve that?

Carolyn
 
G

Greg Maxey

I am assuming that you mean a single "A" or a single "M" in the cell.

Try:
Sub RowCnt()
Dim oTbl As Word.Table
Dim oRows As Rows
Dim pColCnt As Long
Dim i As Long
Dim j As Long
Dim oCell As Word.Range
Dim oCnt As Long
Set oTbl = Selection.Tables(1)
Set oRows = oTbl.Rows
For i = 2 To oRows.Count
pColCnt = oTbl.Rows(i).Cells.Count
For j = 1 To pColCnt
Set oCell = oTbl.Cell(i, j).Range
oCell.MoveEnd wdCharacter, -1
Select Case oCell.Text
Case "M"
oCnt = oCnt + 1
Exit For
Case "A"
oCnt = oCnt + 1
Exit For
Case Else
'Do Nothing
End Select
Next j
Next i
MsgBox oCnt
End Sub
 
K

kaz

Hi Greg
Yes it is for a single M or A. That macro is great! Of course I always
forget to mention at least one thing......

There are two potential columns which can contain these values but I
only want to include the rows where the value is in a specific column -
the 6th column.

Carolyn
 
G

Greg Maxey

OK, try:
Sub RowCnt()
Dim oTbl As Word.Table
Dim oRows As Rows
Dim pColCnt As Long
Dim i As Long
Dim oCell As Word.Range
Dim oCnt As Long
Set oTbl = Selection.Tables(1)
Set oRows = oTbl.Rows
For i = 2 To oRows.Count
If oTbl.Rows(i).Cells.Count >= 6 Then
Set oCell = oTbl.Cell(i, 6).Range
oCell.MoveEnd wdCharacter, -1
Select Case oCell.Text
Case "M"
oCnt = oCnt + 1
Case "A"
oCnt = oCnt + 1
Case Else
'Do Nothing
End Select
End If
Next i
MsgBox oCnt
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top