PDA

View Full Version : [SOLVED:] Word Optionbutton Calculations



Ladyj205
04-03-2018, 01:55 PM
Hi,

I'm very new to this ms word vba. i'm using ms word 2010. I have 4 optionbuttons. I'm trying to get the Score = selected value * 25
i really need some examples step by step. i wont mind if someone use another number besides 25 for examples and steps. im having a very hard time doing this and i need help.

i rename my OptionButtons1 to 4 to:
optionbutton_1_3 (optionbutton1)
optionbutton_1_2 (optionbutton2)
optionbutton_1_1 (optionbutton3)
optionbutton_1_0 (optionbutton4)

i want 0 to be set as default when selecting the optionbutton 0

i want the numbers to appear on the top where it say score and it is set to 0. (SEE IMAGE BELOW)


Dim lngX As Long
Private Sub OptionButton_1_3_Click()
If OptionButton_1_3.Value = True Then
ActiveDocument.Result = 75
End If
ActiveDocument.Fields.Update
End Sub
Private Sub OptionButton_1_2_Click()
If OptionButton_1_2.Value = True Then
ActiveDocument.Result= 50
End If
ActiveDocument.Fields.Update
End Sub
Private Sub OptionButton_1_1_Click()
If OptionButton_1_1.Value = True Then
ActiveDocument.Result = 25
End If
ActiveDocument.Fields.Update
End Sub
Private Sub OptionButton_1_0_Click()
If OptionButton_1_0.Value = True Then
ActiveDocument.Result = 0
End If
ActiveDocument.Fields.Update
End Sub

21965

macropod
04-03-2018, 05:10 PM
Why not use a simple formfield dropdown with options 0-3 and use field coding to multiply whichever option is chosen by 25? Alternatively, use a content control dropdown and an on-exit macro to multiply whichever option is chosen by 25 and output the result to another content control?

gmaxey
04-04-2018, 04:40 AM
One of Paul's suggestions, but if you really want to use option buttons then something like this:


Private Sub OptionButton0_Click()
If OptionButton0 Then
Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 0"
End If
End Sub
Private Sub OptionButton1_Click()
If OptionButton1 Then
Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 25"
End If
End Sub
Private Sub OptionButton2_Click()
If OptionButton2 Then
Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 50"
End If
End Sub
Private Sub OptionButton3_Click()
If OptionButton3 Then
Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 75"
End If
End Sub

Ladyj205
04-04-2018, 07:17 AM
One of Paul's suggestions, but if you really want to use option buttons then something like this:


Private Sub OptionButton0_Click()
If OptionButton0 Then
Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 0"
End If
End Sub
Private Sub OptionButton1_Click()
If OptionButton1 Then
Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 25"
End If
End Sub
Private Sub OptionButton2_Click()
If OptionButton2 Then
Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 50"
End If
End Sub
Private Sub OptionButton3_Click()
If OptionButton3 Then
Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 75"
End If
End Sub



Yes thanks. :)what if i add one more table and rows and columns to this table and a different number say like 20. will it be such as:
Private Sub OptionButton3_Click()
If OptionButton3 Then
Selection.Tables(2).Cell(1, 1).Range.Text = "Scorre = 20"
End If
End Sub


like Selection.Table (2). Cell(2,2) = "Score = 20"

and last but not least what to do if i want to get the total of both tables at the bottom of the tables.

thank u so much this helps me out a whole lot

gmaxey
04-04-2018, 12:38 PM
I can't really guess what you are wanting to do. Show the table structure you want to use. If you want to sum two "results" value the you should put each one in by itself in a definable range (e.g., a cell, bookmark, content control).

Ladyj205
04-04-2018, 01:47 PM
I can't really guess what you are wanting to do. Show the table structure you want to use. If you want to sum two "results" value the you should put each one in by itself in a definable range (e.g., a cell, bookmark, content control).

sorry for the confusion. i just want to put the two values total for a grand total.

gmaxey
04-04-2018, 02:26 PM
So you have 1 table. See attached for one basic process.

Ladyj205
04-05-2018, 01:26 PM
yes thats what im going for.

Ladyj205
04-06-2018, 11:40 AM
So you have 1 table. See attached for one basic process.

if i wanted to add text boxes for the scores. what would i put for the textboxes for each score and grandtotal. i been pulling my hair out trying to figure this out as well.

so i would do something like:

ActiveDocument.InlineShapes(1).OLEFormat.Object.Value

gmaxey
04-06-2018, 05:44 PM
Why would you want to put inline text boxes in the tables? Isn't it working in the demo I provided?

Ladyj205
04-07-2018, 09:38 AM
Why would you want to put inline text boxes in the tables? Isn't it working in the demo I provided?

Yes everything is working. I was told that i needed to put the textboxes for the scores and grandtotal. Just making my life more complicated:crying:

Paul_Hossler
04-07-2018, 06:41 PM
Greg -- typo in your attachment in #7





Function fncCellText(oCell As Cell)



should be




Function fcnCellText(oCell As Cell) ' fcn not fnc






Sub CalcDisplay()
Dim oTbl As Table
Dim arrParts() As Variant
Set oTbl = ActiveDocument.Tables(1)
With oTbl.Cell(1, 1).Range
Select Case True
Case opt_25_3: .Text = "75"
Case opt_25_2: .Text = "50"
Case opt_25_1: .Text = "25"
Case opt_25_0: .Text = "0"
End Select
End With
With oTbl.Cell(6, 1).Range
Select Case True
Case opt_20_3: .Text = "60"
Case opt_20_2: .Text = "40"
Case opt_20_1: .Text = "20"
Case opt_20_0: .Text = "0"
End Select
End With
oTbl.Cell(11, 2).Range.Text = CLng(fcnCellText(oTbl.Cell(1, 1))) + CLng(fcnCellText(oTbl.Cell(6, 1)))
End Sub


Function fncCellText(oCell As Cell)
Dim oRng As Range
Set oRng = oCell.Range
oRng.End = oRng.End - 1
fcnCellText = oRng.Text
End Function

gmaxey
04-08-2018, 09:29 AM
oops. Thanks

Ladyj205
04-09-2018, 07:42 AM
oops. Thanks

So will add ActiveDocument.InlineShapes(1).OLEFormat.Object.Value between case and end select

Sub CalcDisplay()
Dim oTbl As Table
Dim arrParts() As Variant
Set oTbl = ActiveDocument.Tables(1)
With oTbl.Cell(1, 1).Range
Select Case True
Case opt_25_3: .Text = "75"
Case opt_25_2: .Text = "50"
Case opt_25_1: .Text = "25"
Case opt_25_0: .Text = "0"
End Select
ActiveDocument.InlineShapes(1).OLEFormat.Object.Value
End With
With oTbl.Cell(6, 1).Range
Select Case True
Case opt_20_3: .Text = "60"
Case opt_20_2: .Text = "40"
Case opt_20_1: .Text = "20"
Case opt_20_0: .Text = "0"
ActiveDocument.InlineShapes(1).OLEFormat.Object.Value
End Select
End With
oTbl.Cell(11, 2).Range.Text = CLng(fcnCellText(oTbl.Cell(1, 1))) + CLng(fcnCellText(oTbl.Cell(6, 1)))
End Sub


Function fncCellText(oCell As Cell)
Dim oRng As Range
Set oRng = oCell.Range
oRng.End = oRng.End - 1
fcnCellText = oRng.Text
End Function

gmaxey
04-09-2018, 11:02 AM
What is your question?

Ladyj205
04-09-2018, 12:01 PM
What is your question?

I'm trying to add text boxes for the score and grandtotal. i was asking if i add ActiveDocument.InlineShapes(1).OLEFormat.Object.Value inbetween case and end select. will that work? :(

gmaxey
04-09-2018, 12:39 PM
Come on my friend, why don't you simply try and see if it works? It won't work and why you would even imagine it would work is beyond me.

Ladyj205
04-18-2018, 08:58 AM
it works. thank u so much.