perform calculation on userform field

C

colm o'brien

I have a textbox on a user form which asks for a price from the user.
I want this value to be used in a calculation and sales tax calculated from
the gross price and then placed in a bookmark in the letter.

I know how to put the value in the letter but how can i do the calculation.
eg: Premium = 125.45 this is entered as text. how do i convert to currency
and perform calculation (125.45/105*5) to give the sales tax
sales tax is 5% therefore premium is 105% of original price.

If i call salestax ipt i can insert the value at bookmark but how do i
define variables.?
 
G

Graham Mayor

If you want to show the salestax on the userform, add another text box and
lets call it SalesTaxAdded. Then apply the following macro to the textbox
named Premium.

Private Sub Premium_Change()
SalesTaxAdded.Value = Format(Premium.Value * 1.05, "#.00")
End Sub

and as the premium is filled, the salestaxadded field will be filled in real
time.

or

You can calculate the value of the Premium text box with sales tax when you
click the OK button - here CommandButton1 -

Private Sub CommandButton1_Click()
Dim sTaxAdded As String
sTaxAdded = Format(Premium.Value * 1.05, "#.00")
MsgBox sTaxAdded
Unload Me
End Sub

and assign it to the variable sTaxAdded which you can insert into the
document instead of displaying it in a messagebox as shown.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
C

colm o'brien

Graham

Thanks for your input, I have added your code into my userform copy below.
but when i try to run the macro it gives debug error highlighting the line
sTaxAdded = Format(premium.Value * 1.05, "#.00")

have i missed a step or should i have formatted the text box premium
differently

complete code

Private Sub OK_Click()
Dim sTaxAdded As String
sTaxAdded = Format(premium.Value * 1.05, "#.00")
MsgBox sTaxAdded
Unload Me

Dim stradvisor As String

With ActiveDocument
.Bookmarks("Address").Range _
.InsertBefore Address
.Bookmarks("Salutation").Range _
.InsertBefore Salutation
.Bookmarks("Current").Range _
.InsertBefore Current
.Bookmarks("Policydescription").Range _
.InsertBefore Policydescription
.Bookmarks("AlternativeCo").Range _
.InsertBefore AlternativeCo
.Bookmarks("Brokerref").Range _
.InsertBefore Brokerref
.Bookmarks("Rdate").Range _
.InsertBefore Renewaldate
.Bookmarks("premium").Range _
.InsertBefore gross
.Bookmarks("IPT").Range _
.InsertBefore sTaxAdded

If OptionButton1 = True Then stradvisor = "Statement Of Fact"
If OptionButton2 = True Then stradvisor = "Schedule"
.Bookmarks("statementoffact").Range _
.InsertBefore stradvisor
If chain.Value = True Then
' The policy has been placed in a chain with other company.
frmchain.Show
Else
frmsignatory.Show
End If
End With
frmClientdetails.Hide

End Sub
 
G

Graham Mayor

Change premium to the name of your Userform text field that contains the
premium.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Incidentally, it would be better to insert your results INTO the bookmarks
rather than NEXT TO the bookmarks, so that re-running the macro e.g. in case
of error, will replace the content rather than add to it as follows.

Dim rText As Range
With ActiveDocument
Set rText = .Bookmarks("Address").Range
rText.Text = Address
.Bookmarks.Add "Address", rText

Set rText = .Bookmarks("Salutation").Range
rText.Text = Salutation
.Bookmarks.Add "Salutation", rText

Set rText = .Bookmarks("Current").Range
rText.Text = Current
.Bookmarks.Add "Current", rText

Set rText = .Bookmarks("Policydescription").Range
rText.Text = PolicyDescription
.Bookmarks.Add "Policydescription", rText
'etc
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
C

colm o'brien

Thanks for that Graham

That is probably the right thing. as i am only new to this programming word
would you mind explaining the 3 line section for each bookmark ( remarks for
each line)using rtext

thanks
 
G

Graham Mayor

Dim rText As Range 'Define a range variable
With ActiveDocument 'In the active document
'Mark the area occupied by the bookmarked location Address
'as the range rText
Set rText = .Bookmarks("Address").Range
'Define the text content of the range rText
'as the content of your variable Address
rText.Text = Address
'Bookmark the range rText with the original bookmark name
.Bookmarks.Add "Address", rText
'then repeat for each of the rest of the bookmarks
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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