Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-10-2022, 06:45 PM
Formd Formd is offline VBA Adding and Subtracting numbers in word table Windows 7 32bit VBA Adding and Subtracting numbers in word table Office 2007
Advanced Beginner
VBA Adding and Subtracting numbers in word table
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default VBA Adding and Subtracting numbers in word table

Dialog Window.docx
Below is my vba code that i use to calculate a word table. Attached is the Dialog box that the end user use to enter their figures. The code below works ONLY if all the figures entered are positive numbers. However, I do not know if the user will enter negative or positive numbers therefore, I cannot specifically code a field in the dialog box. So, if a user enters a negative code, I would like the dialog box to display the number like (13.00) and have the calculation subtract that number.
ValidateNumber Me.NameOfField will not allow user to insert (13.00) How do i fix this and have to total correctly calculate the numbers?
I hope my explanation is clear.


This is my current Code
Private Sub tbamt1_Change()
ValidateNumber Me.tbamt1
AddTextBox
End Sub
Private Sub tbamt2_Change()
ValidateNumber Me.tbamt2
AddTextBox
End Sub
Private Sub tbamt3_Change()
ValidateNumber Me.tbamt3
AddTextBox
End Sub
Private Sub tbamt4_Change()


ValidateNumber Me.tbamt4
AddTextBox
End Sub
Private Sub tbamt5_Change()
ValidateNumber Me.tbamt5
AddTextBox
End Sub
Private Sub tbamt6_Change()
ValidateNumber Me.tbamt6
AddTextBox
End Sub
Sub ValidateNumber(TextBox)
Dim sTxt As String
sTxt = TextBox.Text
If sTxt = "" Then Exit Sub
If IsNumeric(sTxt) Then
If InStr(sTxt, ".") > 0 Then
If Len(sTxt) - InStr(sTxt, ".") > 2 Then
TextBox.Text = Mid(sTxt, 1, Len(sTxt) - 1)
End If
End If
Exit Sub
End If
TextBox.Text = Mid(sTxt, 1, Len(sTxt) - 1)
End Sub
Private Sub AddTextBox()
With Me
sVal11 = .tbamt1.Value
sVal12 = .tbamt2.Value
sVal13 = .tbamt3.Value
sVal14 = .tbamt4.Value
sVal15 = .tbamt5.Value
sVal16 = .tbamt6.Value
If sVal11 = "" Then sVal11 = 0
If sVal12 = "" Then sVal12 = 0
If sVal13 = "" Then sVal13 = 0
If sVal14 = "" Then sVal14 = 0
If sVal15 = "" Then sVal15 = 0
If sVal16 = "" Then sVal16 = 0
AMOUNTDUE = CDbl(sVal11) + CDbl(sVal12) + CDbl(sVal13) + CDbl(sVal14) _
+ CDbl(sVal15) + CDbl(sVal16)
.tbgtotal = Format(AMOUNTDUE, "$#,##0.00")
End With
End Sub
Reply With Quote
  #2  
Old 04-10-2022, 09:41 PM
Guessed's Avatar
Guessed Guessed is offline VBA Adding and Subtracting numbers in word table Windows 10 VBA Adding and Subtracting numbers in word table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This works for me. I used the Tag property set to "AddMe" on any controls I want to be in the total.
Code:
Private Sub tbamt1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If FormatCell(tbamt1) = True Then SuperAdder
End Sub

Private Sub tbamt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If FormatCell(tbamt2) = True Then SuperAdder
End Sub

Private Sub tbamt3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If FormatCell(tbamt3) = True Then SuperAdder
End Sub

Private Sub tbamt4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If FormatCell(tbamt4) = True Then SuperAdder
End Sub

Private Sub tbamt5_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If FormatCell(tbamt5) = True Then SuperAdder
End Sub

Private Sub tbamt6_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If FormatCell(tbamt6) = True Then SuperAdder
End Sub

Private Function FormatCell(aCtl As Control) As Boolean
  If IsNumeric(aCtl.Value) Then
    aCtl.Value = Format(aCtl.Value, "#,##0.00;(#,##0.00);0")
    FormatCell = True
  ElseIf aCtl = "" Then
    aCtl = 0
    FormatCell = True
  End If
End Function

Private Sub SuperAdder()
  Dim aCtl As Control, dblTotal As Double
  For Each aCtl In Me.Controls
    If aCtl.Tag = "AddMe" Then
      If IsNumeric(aCtl) Then dblTotal = dblTotal + aCtl
    End If
  Next aCtl
  Me.tbgtotal = Format(dblTotal, "#,##0.00;(#,##0.00);0")
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 04-11-2022, 06:28 AM
Formd Formd is offline VBA Adding and Subtracting numbers in word table Windows 7 32bit VBA Adding and Subtracting numbers in word table Office 2007
Advanced Beginner
VBA Adding and Subtracting numbers in word table
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default

Thank you so much for responding. Everything in the code works except it is not totaling up. I get "0" in the total field. What am i missing?
Reply With Quote
  #4  
Old 04-11-2022, 09:41 AM
Formd Formd is offline VBA Adding and Subtracting numbers in word table Windows 7 32bit VBA Adding and Subtracting numbers in word table Office 2007
Advanced Beginner
VBA Adding and Subtracting numbers in word table
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default

The attachment is the table used but only has 0 as a total.
Attached Files
File Type: docx DialogWindow.docx (31.6 KB, 10 views)
Reply With Quote
  #5  
Old 04-11-2022, 03:31 PM
Guessed's Avatar
Guessed Guessed is offline VBA Adding and Subtracting numbers in word table Windows 10 VBA Adding and Subtracting numbers in word table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I can't diagnose things from a picture but I'm guessing you didn't set the tag value on the controls that should be included in the total. For each of those controls, look in the properties window and set the value in the Tag property to AddMe
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 04-11-2022, 05:42 PM
Formd Formd is offline VBA Adding and Subtracting numbers in word table Windows 7 32bit VBA Adding and Subtracting numbers in word table Office 2007
Advanced Beginner
VBA Adding and Subtracting numbers in word table
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Smile

You just lightened a very heavy load.
I completely missed the step in your first post about the "Tag Property".
Once I corrected this on my end, the code works perfectly.
Thank you so much.
Problem solved.
Reply With Quote
  #7  
Old 04-19-2022, 05:44 AM
Formd Formd is offline VBA Adding and Subtracting numbers in word table Windows 7 32bit VBA Adding and Subtracting numbers in word table Office 2007
Advanced Beginner
VBA Adding and Subtracting numbers in word table
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default

I have another issue related to this same problem.

I have a second and third totals to add to the same table.
So I used the following.

Private Sub tbamt1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbamt1) = True Then SuperAdder
End Sub

Private Sub tbamt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbamt2) = True Then SuperAdder
End Sub

Private Sub tbamt3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbamt3) = True Then SuperAdder
End Sub

Private Sub tbamt4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbamt4) = True Then SuperAdder
End Sub

Private Sub tbamt5_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbamt5) = True Then SuperAdder
End Sub

Private Sub tbamt6_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbamt6) = True Then SuperAdder
End Sub

Private Function FormatCell(aCtl As Control) As Boolean
If IsNumeric(aCtl.Value) Then
aCtl.Value = Format(aCtl.Value, "#,##0.00;(#,##0.00);0")
FormatCell = True
ElseIf aCtl = "" Then
aCtl = 0
FormatCell = True
End If
End Function

Private Sub SuperAdder()
Dim aCtl As Control, dblTotal As Double
For Each aCtl In Me.Controls
If aCtl.Tag = "AddMe" Then
If IsNumeric(aCtl) Then dblTotal = dblTotal + aCtl
End If
Next aCtl
Me.tbgtotal = Format(dblTotal, "#,##0.00;(#,##0.00);0")
End Sub



Private Sub tbsubamt1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbsubamt1) = True Then SuperAdd
End Sub
Private Sub tbsubamt2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbsubamt2) = True Then SuperAdd
End Sub
Private Sub tbsubamt3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbsubamt3) = True Then SuperAdd
End Sub
Private Sub tbsubamt4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbsubamt4) = True Then SuperAdd
End Sub
Private Sub SuperAdd()
Dim aCtl As Control, dblTotal As Double
For Each aCtl In Me.Controls
If aCtl.Tag = "Subit" Then
If IsNumeric(aCtl) Then dblTotal = dblTotal + aCtl
End If
Next aCtl
Me.tbsubtot = Format(dblTotal, "$#,##0.00;($#,##0.00);0")
End Sub
Private Sub tbtotalsub_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbtotalsub) = True Then SuperAd
End Sub
Private Sub tbsubtot_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If FormatCell(tbsubtot) = True Then SuperAd
End Sub
Private Sub SuperAd()
Dim aCtl As Control, dblTotal As Double
For Each aCtl In Me.Controls
If aCtl.Tag = "grandt" Then
If IsNumeric(aCtl) Then dblTotal = dblTotal + aCtl
End If
Next aCtl
Me.tbgrantot = Format(dblTotal, "$#,##0.00;($#,##0.00);0")
End Sub



Everything works except the grand total "tbgrantot".
What I am trying to do is sum tbgtotal + tbsubtot = tbgrantot
so I tagged tbgtotal and tbsubtot but nothing shows up in tbgrantot
Help..
Reply With Quote
  #8  
Old 07-11-2022, 03:48 PM
Formd Formd is offline VBA Adding and Subtracting numbers in word table Windows 7 32bit VBA Adding and Subtracting numbers in word table Office 2007
Advanced Beginner
VBA Adding and Subtracting numbers in word table
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default

This thread can be marked as solved.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Adding and Subtracting numbers in word table Adding a symbol to numbers in a table Ulodesk Word VBA 2 10-22-2018 08:05 AM
adding and subtracting time on timesheet renny0927 Excel 1 03-09-2017 01:07 PM
VBA Adding and Subtracting numbers in word table Footer for page numbers recently started showing a colored field when adding page numbers thefonebug Word 12 10-24-2016 05:18 AM
VBA Adding and Subtracting numbers in word table Adding or subtracting in Word danc Word Tables 1 10-29-2013 02:32 PM
Adding captions without numbers (Word 2011 forMac) dkester Word 0 10-01-2013 09:24 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:33 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