#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 40
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 152
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 40
Default 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   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"