Integer question

F

Fuzzhead

In my userform I devide number B by number A and then load that into my
document bookmark "bkRatio1". For an example I get a number like 2.58741. How
do I only load 2.58 into bookmark "bkRatio1"?
 
P

Pesach Shelnitz

Hi,

The following macro will do this if you create the bookmark bkRatio1 with a
number or other text string selected.

Sub InsertNumberInBookmark()
Dim num As Double
Dim newText As String
Dim myRange As Range

num = 2.58741
newText = CStr(Format(num, "##0.00"))
With ActiveDocument
If .Bookmarks.Exists("bkRatio1") = True Then
Set myRange = .Bookmarks("bkRatio1").Range
.Bookmarks("bkRatio1").Range.InsertBefore newText
myRange.Start = .Bookmarks("bkRatio1").Start _
+ Len(newText)
myRange.Delete
Else
MsgBox "The bookmark bkRatio1 was not found."
End If
End With
End Sub

Note that the result is rounded to 2.59. If you want 2.58, you can truncate
the set newText equal to CStr(num) and truncate it using the Left function.
 
F

Fuzzhead

Thanks for the help. It work great.




Pesach Shelnitz said:
Hi,

The following macro will do this if you create the bookmark bkRatio1 with a
number or other text string selected.

Sub InsertNumberInBookmark()
Dim num As Double
Dim newText As String
Dim myRange As Range

num = 2.58741
newText = CStr(Format(num, "##0.00"))
With ActiveDocument
If .Bookmarks.Exists("bkRatio1") = True Then
Set myRange = .Bookmarks("bkRatio1").Range
.Bookmarks("bkRatio1").Range.InsertBefore newText
myRange.Start = .Bookmarks("bkRatio1").Start _
+ Len(newText)
myRange.Delete
Else
MsgBox "The bookmark bkRatio1 was not found."
End If
End With
End Sub

Note that the result is rounded to 2.59. If you want 2.58, you can truncate
the set newText equal to CStr(num) and truncate it using the Left function.
 

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