PDA

View Full Version : [SOLVED:] Simple math with two columns



JinkyJulie
03-23-2015, 11:21 AM
Hello again all,

It has been a while...

I am look for some help with a table and some simple math... for some reason I just cannot get what I want to do to work... (3 column table - 1st col -irrelevant, 2 and 3 cols - single digit numbers that should not be above 2)

Problem has 4 part:


Search for an replace all instance of numbers more than "2" in column(3) with the number "2"
Sum all numbers in column(2) (assign to variable); Sum all numbers in column(3) (assign to variable)
Divide column(3) sum by column(2) sum (assign to variable)
Display result as % (e.g. 74.6%)


Although I have become pretty good with the Word VBA (with your help :)), this simple things has me stomped.

I hope to have made my problem clear.

Thanking you in advance... JJ

gmaxey
03-23-2015, 03:38 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Word.Table
Dim lngIndex As Long, lng2 As Long, lng3 As Long
Set oTbl = ActiveDocument.Tables(1)
For lngIndex = 1 To oTbl.Rows.Count
lng2 = lng2 + Val(oTbl.Cell(lngIndex, 2).Range.Text)
If Val(oTbl.Cell(lngIndex, 3).Range.Text) > 2 Then
oTbl.Cell(lngIndex, 3).Range.Text = 2
End If
lng3 = lng3 + Val(oTbl.Cell(lngIndex, 3).Range.Text)
Next lngIndex
MsgBox lng3 / lng2
End Sub

JinkyJulie
03-24-2015, 11:18 AM
Thank you Mr Maxey...

A couple of small tweaks and it worked!!!

That little push helped me complete a big step...

JJ