Counting rows based on a criteria

P

Peter

Hi,

I have a table where I record faults and errors related to the construction
of a house. Each record is given a priority from 1 to 3. Now, is it possible
to construct another table, and then count the number of priority 1's, 2's
and 3's in the other table (I know about Count() and how to reference a
column in another table, but I can't get it to count records based on a
criteria).

Hopefully someone knows the answer to this one
 
M

macropod

Hi Peter,

You'd be better off doing this on Excel - or at least an Excel worksheet
embedded in your Word document. That way, you'd have access to Excel's
COUNTIF function, which is designed to do this sort of thing. Word's field
functions don't include anything like COUNTIF. Alternatively, you could use
a macro.

Cheers
 
G

Greg Maxey

Peter,

If you want a macro solution you might try the macro below. It assumes
that you have two (2 column) tables in the document. Table 1 list the
problem in column 1 and the priority in column 2. Table 2 has a
heading row and names the priority in column 1 and the count in column
2.

Sub ScratchMacro()
Dim oTbl As Word.Tables
Dim pPri1 As Long
Dim pPri2 As Long
Dim pPri3 As Long
Dim oCell As Cell
Set oTbl = ActiveDocument.Tables
For Each oCell In oTbl(1).Columns(2).Cells
Select Case Val(oCell.Range.Text)
Case 1
pPri1 = pPri1 + 1
Case 2
pPri2 = pPri2 + 1
Case 3
pPri3 = pPri3 + 1
End Select
Next
With oTbl(2)
.Cell(2, 2).Range.Text = pPri1
.Cell(3, 2).Range.Text = pPri2
.Cell(4, 2).Range.Text = pPri3
End With
End Sub
Hi Peter,

You'd be better off doing this on Excel - or at least an Excel worksheet
embedded in your Word document. That way, you'd have access to Excel's
COUNTIF function, which is designed to do this sort of thing. Word's field
functions don't include anything like COUNTIF. Alternatively, you could use
a macro.

Cheers

--
macropod
[MVP - Microsoft Word]


Peter said:
Hi,

I have a table where I record faults and errors related to the construction
of a house. Each record is given a priority from 1 to 3. Now, is it possible
to construct another table, and then count the number of priority 1's, 2's
and 3's in the other table (I know about Count() and how to reference a
column in another table, but I can't get it to count records based on a
criteria).

Hopefully someone knows the answer to this one
 
P

Peter

That worked nicely, thanks!!

Greg Maxey said:
Peter,

If you want a macro solution you might try the macro below. It assumes
that you have two (2 column) tables in the document. Table 1 list the
problem in column 1 and the priority in column 2. Table 2 has a
heading row and names the priority in column 1 and the count in column
2.

Sub ScratchMacro()
Dim oTbl As Word.Tables
Dim pPri1 As Long
Dim pPri2 As Long
Dim pPri3 As Long
Dim oCell As Cell
Set oTbl = ActiveDocument.Tables
For Each oCell In oTbl(1).Columns(2).Cells
Select Case Val(oCell.Range.Text)
Case 1
pPri1 = pPri1 + 1
Case 2
pPri2 = pPri2 + 1
Case 3
pPri3 = pPri3 + 1
End Select
Next
With oTbl(2)
.Cell(2, 2).Range.Text = pPri1
.Cell(3, 2).Range.Text = pPri2
.Cell(4, 2).Range.Text = pPri3
End With
End Sub
Hi Peter,

You'd be better off doing this on Excel - or at least an Excel worksheet
embedded in your Word document. That way, you'd have access to Excel's
COUNTIF function, which is designed to do this sort of thing. Word's field
functions don't include anything like COUNTIF. Alternatively, you could use
a macro.

Cheers

--
macropod
[MVP - Microsoft Word]


Peter said:
Hi,

I have a table where I record faults and errors related to the construction
of a house. Each record is given a priority from 1 to 3. Now, is it possible
to construct another table, and then count the number of priority 1's, 2's
and 3's in the other table (I know about Count() and how to reference a
column in another table, but I can't get it to count records based on a
criteria).

Hopefully someone knows the answer to this one
 

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