Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-16-2022, 08:05 PM
dohertym dohertym is offline Formatting a calculated number - to be added to bookmark Windows 10 Formatting a calculated number - to be added to bookmark Office 2021
Novice
Formatting a calculated number - to be added to bookmark
 
Join Date: May 2022
Posts: 6
dohertym is on a distinguished road
Default Formatting a calculated number - to be added to bookmark

Hi,

I have a userform which requires to the user to enter the number of kilometers travelled. On the word document the number of kms shows but also the cost (i.e. $0.85/km)

I've managed to do the code for this and it's working great. The only issue is that the amount that goes to the bookmarked place for the cost is showiong as 1 decimal place (It's $....).



Can someone please help me with the code I need to format the cost amount correctly?

Below is the code - it shows the number of Kms going to the bookmarked location for that in the document and the cost (once calculated) going to the bookmarked place for it.

Dim KMsTravelled As Range
Set NumberKMs = ActiveDocument.Bookmarks("NumberKms").Range
NumberKMs.Text = Me.KMsTravelled.Value

Dim KMsCost As Range
Set KMsCost = ActiveDocument.Bookmarks("KMsCost").Range
KMsCost.Text = Me.KMsTravelled.Value * 0.85


Thanks for your help
Reply With Quote
  #2  
Old 05-17-2022, 03:11 PM
macropod's Avatar
macropod macropod is offline Formatting a calculated number - to be added to bookmark Windows 10 Formatting a calculated number - to be added to bookmark Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could both simplifiy your code and achieve the desired result with:
Code:
With ActiveDocument
  .Bookmarks("NumberKms").Range.Text = Format(Me.KMsTravelled.Value, "#,##0")
  .Bookmarks("KMsCost").Range.Text = Format(Me.KMsTravelled.Value * 0.85, "$#,##0.00")
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-17-2022, 11:19 PM
dohertym dohertym is offline Formatting a calculated number - to be added to bookmark Windows 10 Formatting a calculated number - to be added to bookmark Office 2021
Novice
Formatting a calculated number - to be added to bookmark
 
Join Date: May 2022
Posts: 6
dohertym is on a distinguished road
Default Error received when I tried the code

Hi Paul,

When I tried the code I received an error.

Se attached screenshot of error message and code.

Not sure what I've done wrong.
Attached Images
File Type: png Error Msg.png (35.9 KB, 11 views)
File Type: png Error Line.png (73.3 KB, 11 views)
Reply With Quote
  #4  
Old 05-18-2022, 12:18 AM
macropod's Avatar
macropod macropod is offline Formatting a calculated number - to be added to bookmark Windows 10 Formatting a calculated number - to be added to bookmark Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Given the line on which the error occurs, it suggests the document you're working on lacks a bookmark named 'KMsCost'.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-18-2022, 04:53 PM
dohertym dohertym is offline Formatting a calculated number - to be added to bookmark Windows 10 Formatting a calculated number - to be added to bookmark Office 2021
Novice
Formatting a calculated number - to be added to bookmark
 
Join Date: May 2022
Posts: 6
dohertym is on a distinguished road
Default

Thanks Paul

Not sure how I missed that
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting a calculated number - to be added to bookmark With macro/userform to replace bookmark only works with number TheBigBoss Word VBA 2 03-27-2022 10:26 PM
Formatting a calculated number - to be added to bookmark Can I use a variable to display the page number of a bookmark ? delboy Word 3 12-29-2020 01:16 PM
Formatting a calculated number - to be added to bookmark I don't understand new formatting marks word has spontaneously added to my document RichardJ Word 5 07-30-2020 11:04 AM
how to get Word to automatically re-number the headings when a new on is added lauped Word 3 07-13-2019 10:32 AM
Formatting a calculated number - to be added to bookmark Add external hyperlink to a calculated number Harleygal17 Excel 4 04-06-2017 11:38 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft