VB code for MS Word 97

  reddwarfcrew 19:22 03 Jul 2004

I have a userform which (amongst other things) will include 2 text boxes and an OK button.

On pressing the OK button I would like the VB code to perform a calculation and return the answer at a bookmark in the document:

IE something like:

Private Sub CommandButton1_Click()

Selection.GoTo Result

TypeText:=Textbox1.value * Textbox2.Value / 1000

End Sub

I would also like the anser to be round to 2 decimal places like (roundup(a1*a2/1000,2) in excel).

Can anyone help?

  reddwarfcrew 20:04 03 Jul 2004

Private Sub CommandButton1_Click()

Dim Res

Selection.GoTo What:=wdGoToBookmark, Name:="Result"

Res = TextBox1.Value * TextBox2.Value \ 1000

Selection.TypeText (Res)

Unload UserForm1


End Sub


The problem is that it only returns the value as an integer and I need it to shows decimals, but rounded up to 2 decimal places.

I think I need to use the 'round' statement, but does this round up or to nearer. Anyway, even if I do it still returns the answer as an integer and not decimals.

On further hunting, I thought I needed to Dim the Res 'As Double', but this hasn't worked either.

  reddwarfcrew 22:13 03 Jul 2004

Private Sub CommandButton1_Click()

Dim Res As Double

Dim SA As Double

Dim Rate As Double

Dim Ans As Double

Dim Res1 As Double

Dim Res2 As Double

Dim Res3 As Double

Dim Res4 As Double

SA = TextBox1.Value * 0.001

Rate = TextBox2.Value

Res = SA * Rate

Res1 = Round(SA * Rate, 0)

Res2 = Res - Res1

Res3 = Res2 * 100

Res4 = Round(Res3, 0)

If Res4 > Res3 Then

Ans = Res4

Ans = Ans * 0.01

Ans = Res1 + Ans


If Res3 - Res4 = 0 Then

Ans = Res4 * 0.01

Ans = Ans + Res1


Ans = Res4 + 1

Ans = Ans * 0.01

Ans = Ans + Res1

End If

End If

Selection.GoTo What:=wdGoToBookmark,


Selection.TypeText (Ans)

Unload UserForm1


End Sub

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

The Evil Within 2 review-in-progress

InVision Studio takes on Adobe XD and Sketch

Camera tips to take better iPhone photos

Comment transformer un iPhone en borne Wi-Fi ?