PDA

View Full Version : Solved: userform and function call



paul_0722
06-28-2008, 03:51 PM
New to Excel VBA and I know I am missing something, but not sure what...

I have a module with this code;


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



and a UserForm1 with this code:

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


and excel sheet with this:


Private Sub cmd_start_Click()
UserForm1.Show
End Sub


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...

Bob Phillips
06-28-2008, 04:11 PM
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

mikerickson
06-28-2008, 04:16 PM
As written AddNumbers will always return 0. The return value is never set
Function AddNumbers(a As Integer) As Integer
AddNumbers = a + 2
End Function

paul_0722
06-28-2008, 05:45 PM
That's it! Thank-you!