VBA to align last digit?

Q

quickquestion

Sometimes in a single column, I have numbers with different numbers of
decimal places, for example whole numbers in most rows and percentages
with one decimal place in every 5th row.

How could I adjust the decimal tabs using vba so that the cells with
one decimal place have tabs shifted to the left so that the last digit
is aligned?

Is there a way to determine the width of one numeric character?

It is pretty easy to count the characters in a cell after the decimal
point.

If I could figure out at run time the width in points or inches of a
single character, then I could move the decimal tab accordingly.

Is there a good way to do this?
Sorry if this question has been asked before.
 
H

Helmut Weber

Hi,

I think all you need is a rightaligned tab
instead of a decimal tab.

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
Q

Quick Question

Helmut Weber said:
Hi,

I think all you need is a rightaligned tab
instead of a decimal tab.

2 reasons I'd rather adjust decimal tabs:

1 - Decimal tabs can align text if there is no explicit "tab"
character in a cell. That is a very handy feature.

2 - I still want closing parentheses, % signs, multiple signs and any
other post number characters to hang over the right side, just like
they do with decimal tabs.

Thanks for your suggestion though. If anyone knows how to figure out
the width of one numeric character at run-time, I'd still really
appreciate it.

Do most fonts have monospace numbers? If so, maybe the way to go
would be to store a list of relations between font number width and
point size for different fonts that I use and look it up at run time.

Any other suggestions?
 
J

Jonathan West

Quick Question said:
2 reasons I'd rather adjust decimal tabs:

1 - Decimal tabs can align text if there is no explicit "tab"
character in a cell. That is a very handy feature.

2 - I still want closing parentheses, % signs, multiple signs and any
other post number characters to hang over the right side, just like
they do with decimal tabs.

Thanks for your suggestion though. If anyone knows how to figure out
the width of one numeric character at run-time, I'd still really
appreciate it.


Position the cursor before the digit.
Read the value of Selection.Information(wdHorizontalPositionRelativeToPage)
Move the cursor after the digit
Read the value of Selection.Information(wdHorizontalPositionRelativeToPage)

The difference between the two values is the width of the character in
points.
 
H

Helmut Weber

Hi,
only in explicitly monospaced fonts, I'd say, do digits
have the same width. Unless You want to take off an extra
half year and study how two extract data from font files,
you may very approximately (!) get the width of characters
by placing the cursor left of a digit, reading the
cursor position, placing it right of a digit, reading
the cursor position again, and calculating the difference.
Like, if the cursor is at the start of "1234567890":

Sub test491()
Dim i As Integer
Dim x1 As Single
Dim x2 As Single
For i = 1 To 10
x1 = Selection.Information(wdHorizontalPositionRelativeToPage)
Selection.MoveRight
x2 = Selection.Information(wdHorizontalPositionRelativeToPage)
MsgBox x2 - x1
Next
End Sub

Try with different screen resolution, with different
zoom factors, with different printer drivers. No luck it in!
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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