Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

VBA to align last digit?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
quickquestion@mailinator.com - 02 Dec 2004 06:56 GMT
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.
Helmut Weber - 02 Dec 2004 09:12 GMT
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
Quick Question - 02 Dec 2004 16:10 GMT
> 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?
Jonathan West - 02 Dec 2004 16:27 GMT
>> Hi,
>>
[quoted text clipped - 13 lines]
> 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.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Helmut Weber - 02 Dec 2004 16:58 GMT
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/

>> Hi,
>>
[quoted text clipped - 19 lines]
>
>Any other suggestions?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.