command button calculation

bronwyn

Board Regular
Joined
Jun 12, 2004
Messages
93
hi all, what im trying to achieve is to have a command button, which will take input from a textbox, and calculate results and display in textbox2. the calculation will be whatever number is entered in textbox1, divide this number by 18 and multiply it by 40. i know i can achieve this with formulas, but need to use vba and commands from toolbox to achieve this, as alwasy any help appreciated
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

fridgenep

Board Regular
Joined
Mar 26, 2006
Messages
73
Hi bronwyn,

try this one in a seperate module:

Code:
Option Explicit

Sub test()
 If IsNumeric(ActiveSheet.TextBox1.Value) And IsEmpty(ActiveSheet.TextBox1) = False Then
   ActiveSheet.TextBox2.Value = ActiveSheet.TextBox1.Value / 18 * 40
 Else
   MsgBox "Please check TextBox1"
   ActiveSheet.TextBox2.Value = ""
 End If
End Sub

EDIT: I think you´re able to chang my code for working with a command button... If not:
Code:
Option Explicit

Private Sub CommandButton1_Click()
 If IsNumeric(ActiveSheet.TextBox1.Value) And IsEmpty(ActiveSheet.TextBox1) = False Then
   ActiveSheet.TextBox2.Value = ActiveSheet.TextBox1.Value / 18 * 40
 Else
   MsgBox "Please check TextBox1"
   ActiveSheet.TextBox2.Value = ""
 End If
End Sub
 

irresistible007

Board Regular
Joined
Nov 24, 2005
Messages
173
assuming that your command button is named "cmd_Calculate"... Just kick the following code:

Private Sub Cmd_Calculate()
TextBox2.Text = TextBox1.Text / 18 * 40
End Sub
 

Forum statistics

Threads
1,136,597
Messages
5,676,719
Members
419,647
Latest member
usas12gthr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top