Assigning Value to Global variable

albatross

New Member
Joined
Nov 9, 2013
Messages
3
Hi,
I'm new to excel VBA. Hope someone can help me.
I am assigning value to a textbox inside a subroutine. There are 16 such scenarios. Now I want to add up all base and all Pand I values and store them in a different textbox. I was thinking of using a global variable but I've been unable to do it. Can someone help me?

Private Sub txtBase2_Change()

If Len(txtBase2.Text) <> 0 Then
dblBase = txtBase2.Value
End If


If Len(txtPandI2.Text) <> 0 Then
dblPandI = txtPandI2.Value
End If


dblTotal = dblBase + dblPandI
txtTotal2.Value = dblTotal

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about something like

Code:
Dim i as Long
    
txtBaseTotal.Text = vbNullString
txtPandiTotal.Text = vbNullString
    
For i = 1 to 16
    txtBaseTotal.Text = CStr(Val(txtBaseTotal.Text) + Val(Me.Controls("txtBase" & i).Text))
    txtPandiTotal.Text = CStr(Val(txtPandiTotal.Text) + Val(Me.Controls("txtPandi" & i).Text))
Next i
 
Last edited:
Upvote 0
Hi Mike, Thanks for your prompt reply. But I actually found a way around this. I can tackle it in a different manner. However, I did try your suggestion but I was getting some random values, not the exact sum. I still believe that if i can declare a global variable, initialise it and then update it for each of the 16 cases, I'll have the sum and I can then use it as per my choice.
 
Upvote 0
For my code, check the spelling and capitalization. Are all the controls named consistently including upper/lower case of letters?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top