Results 1 to 4 of 4

Assigning Value to Global variable

This is a discussion on Assigning Value to Global variable within the Excel Questions forums, part of the Question Forums category; Hi, I'm new to excel VBA . Hope someone can help me. I am assigning value to a textbox inside ...

  1. #1
    New Member
    Join Date
    Nov 2013
    Posts
    3

    Default Assigning Value to Global variable

    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

  2. #2
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    16,679

    Default Re: Assigning Value to Global variable

    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 by mikerickson; Nov 9th, 2013 at 01:09 PM.

  3. #3
    New Member
    Join Date
    Nov 2013
    Posts
    3

    Default Re: Assigning Value to Global variable

    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.

  4. #4
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    16,679

    Default Re: Assigning Value to Global variable

    For my code, check the spelling and capitalization. Are all the controls named consistently including upper/lower case of letters?

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com