Consulting

Results 1 to 4 of 4

Thread: Solved: userform and function call

  1. #1

    Solved: userform and function call

    New to Excel VBA and I know I am missing something, but not sure what...

    I have a module with this code;

    [VBA]
    Function AddNumbers(a As Integer) As Integer
    Dim b As Integer
    b = a + 2
    End Function

    [/VBA]

    and a UserForm1 with this code:

    [VBA]Public Sub cmd_submit_Click()
    Dim a As Integer
    Dim b As Integer

    a = txt_input.Value
    AddNumbers (a)
    lbl_text.Caption = b
    End Sub
    [/VBA]

    and excel sheet with this:

    [VBA]
    Private Sub cmd_start_Click()
    UserForm1.Show
    End Sub
    [/VBA]

    When I click my worksheet button, the userform1 shows up, I input a number and press submit button, but the lbl.text.caption always shows "0" and not the number "b" (numbers added together) as I expected.

    I suspect I have the dim statements mixed up but I cannot seem to get it right

    Any help for a newbie appreciated...

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub cmd_submit_Click()
    Dim a As Integer
    Dim b As Integer

    a = txt_input.Value
    b = AddNumbers (a)
    lbl_text.Caption = b
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    As written AddNumbers will always return 0. The return value is never set
    [VBA]Function AddNumbers(a As Integer) As Integer
    AddNumbers = a + 2
    End Function[/VBA]

  4. #4
    That's it! Thank-you!

Posting Permissions

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