PDA

View Full Version : Powerpoint calculation



William4728
05-12-2024, 08:53 PM
Helloo everyone, i am new here. And I do not have any experience with vab, I need help with some coding please i have a powerpoint game with 40 slides each slide has a points counter I used an active x lable for theses there are two counters per slide. the counters are named Label 11 and Lable 12 The end slide is number 41 and i have a scoreboard in it all i want to do is to display the total number of points in the scoreboard for Lable 11 And Lable 12 so basically just adding up all the points i have added a png of the scoreboard I hope someone can help

Aussiebear
05-12-2024, 11:48 PM
Welcome to VBAX William4728. I have moved your thread to the Powerpoint section, hopefully you will get some assistance here.

jdelano
05-13-2024, 06:53 AM
You can place the variables for the total boys and girls scores in a module, when increasing or decreasing it on each slide's total also do it to these public variables, this will make the values available to the last slide to display it there.

Module1


Public totalBoysScore as Integer
Public totalGirlsScore as Integer

William4728
05-13-2024, 06:29 PM
You can place the variables for the total boys and girls scores in a module, when increasing or decreasing it on each slide's total also do it to these public variables, this will make the values available to the last slide to display it there.

Module1


Public totalBoysScore as Integer
Public totalGirlsScore as Integer



Hi thank you for your reply but it does not work, Maybe i was not clear on what I need. I also send you pictures of the slide and total slide and the code i am using to change the points on each slide.

All I require is for lable 11 and Label 12 to show the grand total of points for all the slides and display it in the scoreboard


Here is the code i am using to change the points in each slide


Sub Label1Plus1()
Label1.Caption = (Label1.Caption) + 1
End Sub

Sub Label1Minus1()
Label1.Caption = (Label1.Caption) - 1
End Sub

Sub Label2Minus1()
Label2.Caption = (Label2.Caption) - 1
End Sub

Sub Label2Plus1()
Label2.Caption = (Label2.Caption) + 1
End Sub

Sub Label1Reset()
Label1.Caption = 0
End Sub

Sub ExitPPT()
ActivePresentation.SlideShowWindow.View.Exit
End Sub

Sub Reset()
Label1.Caption = 0
Label2.Caption = 0
End Sub


Private Sub Label1_Click()

End Sub


Private Sub Label2_Click()

End Sub
as i have indicated in the picture scoreboard

jdelano
05-14-2024, 02:15 AM
In the code where you do plus or minus, you need to add the totalBoysScore = totalBoysScore + 1 or totalBoysScore = totalBoysScore - 1 (same with the girls score) then you use the amounts in these variables to fill the last slide.

William4728
05-15-2024, 09:08 PM
I have tried your surggestion but it does not do what I want it to do, It only allows me to increase or decrese the points by a factor of +/- 1 I already have that part working. All I need is to total Slide 1 to 40 Label 11 and show this figure in the Scoreboard and the same for Label 12

jdelano
05-16-2024, 01:03 AM
If each slide increases (or decreases depending on what is needed) the global total variable as well as its own, then the global variable will be a sum of all slides.
An alternative would be to loop though all slides and read the label values.

You could insert a Module and create:


Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)

Dim totalBoysScore As Integer
Dim totalGirlsScore As Integer


Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation

For Each shp In sld.Shapes
If shp.Name = "Label11" Then

If sld.Name = "Slide41" Then
' this is the last slide, put the totals in the labels
shp.TextFrame.TextRange.Text = totalBoysScore
Else
totalBoysScore = totalBoysScore + Val(shp.TextFrame.TextRange.Text)
End If

ElseIf shp.Name = "Label12" Then

If sld.Name = "Slide41" Then
' this is the last slide, put the totals in the labels
shp.TextFrame.TextRange.Text = totalGirlsScore
Else
totalGirlsScore = totalGirlsScore + Val(shp.TextFrame.TextRange.Text)
End If

End If
Next shp
Next sld



End Sub


This event runs every time a slide is, well, changed during the presentation. This isn't completely tested just trying to give an alternate idea

William4728
05-16-2024, 01:43 AM
Thank you for your help, so I add this code to a new module or add it to my existing code if the later does it go at the begining or the end and do I have to add the code to all the slides sorry to be a pain just no great understanding of VBA

jdelano
05-16-2024, 02:11 AM
You can put this in any Module file (those listed under the Modules section on the left project pane)
No worries, this stuff can be tough to get your head around, especially this event as it isn't documented.

William4728
05-19-2024, 09:21 PM
Hi Jdelano, I have tried to get this to work over the weekend but nothing happens If i make a video clip of how my slides work showing you how it is set up. Will this site allow to up load it to you maybe I am doing something wrong, I put the code in a new module and also tried adding to the existing code nothing happens.

Aussiebear
05-19-2024, 09:24 PM
You can always try to attach a PPT to your post. Click on Go advanced, Manage Attachments and follow the prompts from there.

William4728
05-19-2024, 10:31 PM
I just tried to upload the ppt file I keep fetting the error cant upload the file ppt.

William4728
05-19-2024, 10:41 PM
Ok I have shortened the file because it was too big but in the original file there are 41 slides with 41 being the scoreboard

jdelano
05-20-2024, 02:23 AM
Thanks, I just about have it sorted.

EDIT:
Okay, I added Module1 (as previously described) then added the global variables for girl's and boy's totals. Then I added a public sub procedure that updates both the totals in the vars, and the total displayed in the labels on Slide41; there weren't any labels for the scores on that slide, so I added a label1 and label2 by copying them from another slide.

Code in Module1


' global variable holding thee total points
Public totalBoysScore As Integer
Public totalGirlsScore As Integer


Public Sub AdjustTotals(whichOne As String, UpOrDown As String)


' adjust the global variables accordingly
Select Case whichOne

Case "Girls"
If UpOrDown = "Up" Then
totalGirlsScore = totalGirlsScore + 1
Else
totalGirlsScore = totalGirlsScore - 1
End If
Case "Boys"
If UpOrDown = "Up" Then
totalBoysScore = totalBoysScore + 1
Else
totalBoysScore = totalBoysScore - 1
End If

End Select

' write the total to the proper labels on slide41
Dim lastSlide As Slide
Set lastSlide = ActivePresentation.Slides(ActivePresentation.Slides.Count)

' because the shape is actually an ActiveX label, you have to
' access the caption property which is different from a normal shape
lastSlide.Shapes("Label1").OLEFormat.Object.Caption = totalGirlsScore
lastSlide.Shapes("Label2").OLEFormat.Object.Caption = totalBoysScore

Set lastSlide = Nothing ' release the slide object
End Sub





I modified the code in Slide1 (start), Slide2 and Slide 3

Slide1: I changed the start action to a Macro to reset all score labels to 0 as well as the global vars


Sub ResetAllScores()
' on start reset all score labels to 0 and the global variables that hold the totals
Dim sld As Slide

' the first slide doesn't have label1 and 2 - ignore the error that they aren't there
On Error Resume Next
For Each sld In ActivePresentation.Slides

' because the shape is actually an ActiveX label, you have to
' access the caption property which is different from a normal shape

sld.Shapes("Label1").OLEFormat.Object.Caption = 0
sld.Shapes("Label2").OLEFormat.Object.Caption = 0

Next sld


On Error Resume Next

totalGirlsScore = 0
totalBoysScore = 0


SlideShowWindows(1).View.Next ' move to the next slide

End Sub






Slide2 and 3 have similar code so I'll just show Slide2 modifications


Sub Label1Plus1()
Label1.Caption = (Label1.Caption) + 1 ' increase the girls score for this slide
AdjustTotals "Girls", "Up" ' increase the girls score for the last slide
End Sub
Sub Label1Minus1()
Label1.Caption = (Label1.Caption) - 1 ' decrease the girls score for this slide
AdjustTotals "Girls", "Down" ' decrease the girls score for the last slide

End Sub
Sub Label2Minus1()
Label2.Caption = (Label2.Caption) - 1 ' decrease the boys score for this slide
AdjustTotals "Boys", "Down" ' decrease the boys score for the last slide

End Sub
Sub Label2Plus1()
Label2.Caption = (Label2.Caption) + 1 ' increase the girls score for this slide
AdjustTotals "Boys", "Up" ' increase the boys score for the last slide
End Sub





Now, you could clean this up more by just calling AdjustTotals and send a reference to the label on the slide, that way you would just call AdjustTotals and it would handle all the scoring needs, but I didn't want to get too far into the weeds.