Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.misc
|
|||
|
|||
VBA Command Button
I am trying to develop a very very simple calculator to calculate the amount
of shares of stock I can afford. I have three text boxes and a command button... The first text box is for entering the amount of money I have to spend. The second box is for entering the price of a share of stock. The third box is supposed to display the amount of shares I can afford. Basically just need the third box to divide my (money on hand) / (share price) , when I click a command button. How would I program this in VBA? Any links or examples to learn how to do this on my own? I know this is simple, but don't know VBA. I have $1000 on hand and the stock is $46 a share. When I click calculate I want to display in the third text box that I can purchase 21.74 shares. Thanks! |
#2
Posted to microsoft.public.excel.misc
|
|||
|
|||
VBA Command Button
I'm kind of assuming this is on a user form? Although it really doesn't
matter too much. What has to happen is that your _Click event for the command button must retrieve the values in the two text boxes, do the math and then place the result into your third text box. In the simplest form, the code for the button's _Click command could be: Private Sub CommandButton1_Click() TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub actually you could write the line of code as TextBox3.Text = TextBox1.Text / TextBox2.Text and get the result, but I prefer using the Val() function to give others down the road an idea that I'm expecting numbers to be entered into the first two text boxes. In either case, if someone types "hello" into the first box and "world" into the second, the code will fail with a type mismatch (hard to multiply words), so a robust solution would include error handling. The following revision to the code will not only catch cases where non-numeric entries are made, but will tell where the problem is and put the user back into the (first) text box needing a corrected entry: Private Sub CommandButton1_Click() If Not IsNumeric(TextBox1.Text) Then MsgBox "Please enter an amount into TextBox1" TextBox1.SetFocus Exit Sub End If If Not IsNumeric(TextBox2.Text) Then MsgBox "Please enter an amount into TextBox2" TextBox2.SetFocus Exit Sub End If TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub "IntricateFool" wrote: I am trying to develop a very very simple calculator to calculate the amount of shares of stock I can afford. I have three text boxes and a command button... The first text box is for entering the amount of money I have to spend. The second box is for entering the price of a share of stock. The third box is supposed to display the amount of shares I can afford. Basically just need the third box to divide my (money on hand) / (share price) , when I click a command button. How would I program this in VBA? Any links or examples to learn how to do this on my own? I know this is simple, but don't know VBA. I have $1000 on hand and the stock is $46 a share. When I click calculate I want to display in the third text box that I can purchase 21.74 shares. Thanks! |
#3
Posted to microsoft.public.excel.misc
|
|||
|
|||
VBA Command Button
Why? It would be so much simpler to make it a live computation.
Lou "JLatham" wrote: I'm kind of assuming this is on a user form? Although it really doesn't matter too much. What has to happen is that your _Click event for the command button must retrieve the values in the two text boxes, do the math and then place the result into your third text box. In the simplest form, the code for the button's _Click command could be: Private Sub CommandButton1_Click() TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub actually you could write the line of code as TextBox3.Text = TextBox1.Text / TextBox2.Text and get the result, but I prefer using the Val() function to give others down the road an idea that I'm expecting numbers to be entered into the first two text boxes. In either case, if someone types "hello" into the first box and "world" into the second, the code will fail with a type mismatch (hard to multiply words), so a robust solution would include error handling. The following revision to the code will not only catch cases where non-numeric entries are made, but will tell where the problem is and put the user back into the (first) text box needing a corrected entry: Private Sub CommandButton1_Click() If Not IsNumeric(TextBox1.Text) Then MsgBox "Please enter an amount into TextBox1" TextBox1.SetFocus Exit Sub End If If Not IsNumeric(TextBox2.Text) Then MsgBox "Please enter an amount into TextBox2" TextBox2.SetFocus Exit Sub End If TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub "IntricateFool" wrote: I am trying to develop a very very simple calculator to calculate the amount of shares of stock I can afford. I have three text boxes and a command button... The first text box is for entering the amount of money I have to spend. The second box is for entering the price of a share of stock. The third box is supposed to display the amount of shares I can afford. Basically just need the third box to divide my (money on hand) / (share price) , when I click a command button. How would I program this in VBA? Any links or examples to learn how to do this on my own? I know this is simple, but don't know VBA. I have $1000 on hand and the stock is $46 a share. When I click calculate I want to display in the third text box that I can purchase 21.74 shares. Thanks! |
#4
Posted to microsoft.public.excel.misc
|
|||
|
|||
VBA Command Button
It fits the model he described: two input boxes, one button, one output box.
"Rookie 1st class" wrote: Why? It would be so much simpler to make it a live computation. Lou "JLatham" wrote: I'm kind of assuming this is on a user form? Although it really doesn't matter too much. What has to happen is that your _Click event for the command button must retrieve the values in the two text boxes, do the math and then place the result into your third text box. In the simplest form, the code for the button's _Click command could be: Private Sub CommandButton1_Click() TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub actually you could write the line of code as TextBox3.Text = TextBox1.Text / TextBox2.Text and get the result, but I prefer using the Val() function to give others down the road an idea that I'm expecting numbers to be entered into the first two text boxes. In either case, if someone types "hello" into the first box and "world" into the second, the code will fail with a type mismatch (hard to multiply words), so a robust solution would include error handling. The following revision to the code will not only catch cases where non-numeric entries are made, but will tell where the problem is and put the user back into the (first) text box needing a corrected entry: Private Sub CommandButton1_Click() If Not IsNumeric(TextBox1.Text) Then MsgBox "Please enter an amount into TextBox1" TextBox1.SetFocus Exit Sub End If If Not IsNumeric(TextBox2.Text) Then MsgBox "Please enter an amount into TextBox2" TextBox2.SetFocus Exit Sub End If TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub "IntricateFool" wrote: I am trying to develop a very very simple calculator to calculate the amount of shares of stock I can afford. I have three text boxes and a command button... The first text box is for entering the amount of money I have to spend. The second box is for entering the price of a share of stock. The third box is supposed to display the amount of shares I can afford. Basically just need the third box to divide my (money on hand) / (share price) , when I click a command button. How would I program this in VBA? Any links or examples to learn how to do this on my own? I know this is simple, but don't know VBA. I have $1000 on hand and the stock is $46 a share. When I click calculate I want to display in the third text box that I can purchase 21.74 shares. Thanks! |
#5
Posted to microsoft.public.excel.misc
|
|||
|
|||
VBA Command Button
Worked as needed....
Thank you! "JLatham" wrote: I'm kind of assuming this is on a user form? Although it really doesn't matter too much. What has to happen is that your _Click event for the command button must retrieve the values in the two text boxes, do the math and then place the result into your third text box. In the simplest form, the code for the button's _Click command could be: Private Sub CommandButton1_Click() TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub actually you could write the line of code as TextBox3.Text = TextBox1.Text / TextBox2.Text and get the result, but I prefer using the Val() function to give others down the road an idea that I'm expecting numbers to be entered into the first two text boxes. In either case, if someone types "hello" into the first box and "world" into the second, the code will fail with a type mismatch (hard to multiply words), so a robust solution would include error handling. The following revision to the code will not only catch cases where non-numeric entries are made, but will tell where the problem is and put the user back into the (first) text box needing a corrected entry: Private Sub CommandButton1_Click() If Not IsNumeric(TextBox1.Text) Then MsgBox "Please enter an amount into TextBox1" TextBox1.SetFocus Exit Sub End If If Not IsNumeric(TextBox2.Text) Then MsgBox "Please enter an amount into TextBox2" TextBox2.SetFocus Exit Sub End If TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub "IntricateFool" wrote: I am trying to develop a very very simple calculator to calculate the amount of shares of stock I can afford. I have three text boxes and a command button... The first text box is for entering the amount of money I have to spend. The second box is for entering the price of a share of stock. The third box is supposed to display the amount of shares I can afford. Basically just need the third box to divide my (money on hand) / (share price) , when I click a command button. How would I program this in VBA? Any links or examples to learn how to do this on my own? I know this is simple, but don't know VBA. I have $1000 on hand and the stock is $46 a share. When I click calculate I want to display in the third text box that I can purchase 21.74 shares. Thanks! |
#6
Posted to microsoft.public.excel.misc
|
|||
|
|||
VBA Command Button
Glad to hear it. And thanks for the feedback, always appreciated.
"IntricateFool" wrote: Worked as needed.... Thank you! "JLatham" wrote: I'm kind of assuming this is on a user form? Although it really doesn't matter too much. What has to happen is that your _Click event for the command button must retrieve the values in the two text boxes, do the math and then place the result into your third text box. In the simplest form, the code for the button's _Click command could be: Private Sub CommandButton1_Click() TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub actually you could write the line of code as TextBox3.Text = TextBox1.Text / TextBox2.Text and get the result, but I prefer using the Val() function to give others down the road an idea that I'm expecting numbers to be entered into the first two text boxes. In either case, if someone types "hello" into the first box and "world" into the second, the code will fail with a type mismatch (hard to multiply words), so a robust solution would include error handling. The following revision to the code will not only catch cases where non-numeric entries are made, but will tell where the problem is and put the user back into the (first) text box needing a corrected entry: Private Sub CommandButton1_Click() If Not IsNumeric(TextBox1.Text) Then MsgBox "Please enter an amount into TextBox1" TextBox1.SetFocus Exit Sub End If If Not IsNumeric(TextBox2.Text) Then MsgBox "Please enter an amount into TextBox2" TextBox2.SetFocus Exit Sub End If TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub "IntricateFool" wrote: I am trying to develop a very very simple calculator to calculate the amount of shares of stock I can afford. I have three text boxes and a command button... The first text box is for entering the amount of money I have to spend. The second box is for entering the price of a share of stock. The third box is supposed to display the amount of shares I can afford. Basically just need the third box to divide my (money on hand) / (share price) , when I click a command button. How would I program this in VBA? Any links or examples to learn how to do this on my own? I know this is simple, but don't know VBA. I have $1000 on hand and the stock is $46 a share. When I click calculate I want to display in the third text box that I can purchase 21.74 shares. Thanks! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|