Type mismatch again

J

Joanne

Hello,
I feel so frustrated with myself in not understanding values within cells
vs. the cells themselves. I am trying to total a column in a table in Word
2003. Some of the cells may be empty. The total will be put in cell E19 (at
least in this case). When some people tested the macro, we found that it is
not totaling the cells if the user placed a $ in front of the numeric value,
so I thought I should make sure that the cell's values are seen as numeric.
Here's what I did. I tried the format with both .range just alone and also
..range.text and it still gives a type mismatch.
Any help would be greatly, greatly appreciated.


Sub ConvertToCurrencyAndAdvance()
Dim i As Long, j As Long, vSum As Long
Dim oNum As Range
vSum = 0

If Not Selection.Information(wdWithInTable) Then
MsgBox "Please place the cursor inside the table & restart macro"
Exit Sub
End If
i = ActiveDocument.Tables(1).Rows.Count

For j = 9 To i

With Selection.Tables(1)
Set oNum = .Cell(i, 5).Range
oNum.End = oNum.End - 1
MsgBox oNum
.Cell(i, 5).Range.Text = FormatCurrency(Expression:=oNum, _
NumDigitsAfterDecimal:=2, IncludeLeadingDigit:=vbTrue, _
UseParensForNegativeNumbers:=vbTrue)
End With

'vSum = vSum + Val(ActiveDocument.Tables(1).Cell(Row:=i,
Column:=5).Range.Text)
MsgBox vSum
Next j
'
'myTable.Cell(i, 5).Range.InsertAfter (vSum)
End Sub
 
G

Greg Maxey

Joanne,

You are learning that cell ranges are tricky ;-)

Sub ConvertToCurrencyAndAdvance()
Dim i As Long, j As Long, vSum As Double
Dim oNum As Range
Dim myTable As Word.Table
vSum = 0
If Not Selection.Information(wdWithInTable) Then
MsgBox "Please place the cursor inside the table & restart macro"
Exit Sub
Else
Set myTable = Selection.Tables(1)
End If
i = myTable.Rows.Count
For j = 9 To i - 1
With myTable
'Get the value of the cell
Set oNum = .Cell(j, 5).Range
'Strip end of cell marker
oNum.End = oNum.End - 1
'If cell value is numeric then format
If IsNumeric(oNum) Then
.Cell(j, 5).Range.Text = FormatCurrency(Expression:=oNum, _
NumDigitsAfterDecimal:=2, IncludeLeadingDigit:=vbTrue, _
UseParensForNegativeNumbers:=vbTrue)
'Get the new cell value
Set oNum = .Cell(j, 5).Range
'Strip the end of cell marker
oNum.End = oNum.End - 1
'Add it up
vSum = vSum + CDbl(Mid(oNum, 2, Len(oNum) - 1))
End If
End With
Next j
myTable.Cell(i, 5).Range.Text = FormatCurrency(Expression:=vSum, _
NumDigitsAfterDecimal:=2, IncludeLeadingDigit:=vbTrue, _
UseParensForNegativeNumbers:=vbTrue)
End Sub
 
G

Greg Maxey

You don't have to strip the "$" out of the summing equation:

vSum = vSum + CDbl(oNum)

Works also.
 

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