Tech Eval

G

Greg Maxey

Masters,

Earlier I posted a response in the Tables NG. The OP was asking how to
right a macro to add 4 to the value in column A and put the result in column
B. The OP was looking for a macro to avoid entering repetitive formulas in
his table. I came up with the following which seems to work, but I am
wondering if it is efficient and the correct approach. Thanks:

Sub ScratchMacro()
Dim i As Long
Dim aValue As Range
Dim bValue As Range
Dim oTbl As Table
Set oTbl = Selection.Tables(1)
For i = 1 To oTbl.Rows.Count
Set aValue = oTbl.Cell(i, 1).Range
If IsNumeric(aValue) Then
With aValue
.MoveEnd Unit:=wdCharacter, Count:=-1
End With
Set bValue = oTbl.Cell(i, 2).Range
bValue = aValue + 4
End If
Next

End Sub
 
D

Doug Robbins - Word MVP

Hi Greg,

I would use:

Dim i As Long, aval As Range, mytable As Table
Set mytable = Selection.Tables(1)
For i = 1 To mytable.Rows.Count
Set aval = mytable.Cell(i, 1).Range
aval.End = aval.End - 1
If IsNumeric(aval) Then
mytable.Cell(i, 2).Range.Text = aval + 4
End If
Next i


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jay Freedman

Hi Greg,

Just to be picky, you shouldn't rely on .Text being the default
property of the Range object, or on the automatic conversion of a
string to a number. It should be

If IsNumeric(aval.Text) Then
mytable.Cell(i, 2).Range.Text = CInt(aval.Text) + 4
End If

or use CSng if the value in aval could have decimal places.
 
D

Doug Robbins - Word MVP

But just to be picky, if .Text isn't the default property of a .Range, what
is. Or, perhaps the question should be "Under what circumstances might
..Text not be the default property of a range?" If there are none, why
shouldn't you rely on the default property?

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Jay Freedman said:
Hi Greg,

Just to be picky, you shouldn't rely on .Text being the default
property of the Range object, or on the automatic conversion of a
string to a number. It should be

If IsNumeric(aval.Text) Then
mytable.Cell(i, 2).Range.Text = CInt(aval.Text) + 4
End If

or use CSng if the value in aval could have decimal places.
 
J

Jay Freedman

But just to be picky, if .Text isn't the default property of a .Range, what
is. Or, perhaps the question should be "Under what circumstances might
.Text not be the default property of a range?" If there are none, why
shouldn't you rely on the default property?

Hi Doug,

Maybe "rely on" isn't the right phrase... What I had in mind is "the
whole idea of 'default property' is an abomination and shouldn't be
used". :) The reason to use explicit properties is mainly for human
understanding. I don't want to have to remember what the default
propery is for each of a few dozen object types.

Down the road, who knows what Microsoft may pull on us? Note that
there are no default properties in C# -- a design decision that I
endorse completely.
 
W

Word Heretic

G'day Jay Freedman <[email protected]>,

Amen. It has taught me some nasty habits I find really hard to break.
The most common one I see / use erroneously is ActiveDocument.<assumed
content story range>


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Jay Freedman reckoned:
 

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