VBA code to substract 2 numbers and display results

greatcookies

Board Regular
Joined
Sep 25, 2005
Messages
187
I need VBA code to substract 2 numbers in my excel sheet and display the results in a message box. I will acitvate this through the use of a command button. Can anyone help with proper code to achieve this.

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
Well I know you can display the result of a cell in a text box... you can click the outside border of a textbox and the go to the edit bar and enter something like: =F1
Then the Value is entered in your text box. Maybe you can have the formula hidden on your sheet somewhere.

Michael
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
Code:
Sub SubtractTwoCells()
    MsgBox Range("B7").Value - Range("B8").Value
End Sub
 

greatcookies

Board Regular
Joined
Sep 25, 2005
Messages
187
Thanks John!

I tried this and apparently i am doing something wrong. I placed in a module for that sheet and attached it to a commande button. When i run I get a run-time error '13' Type mismatch. Any ideas on how to correct?
Greatly apprecitate your help!
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985

ADVERTISEMENT

Just includ the MSgbox line in your command button code. Like:
Code:
Private Sub CommandButton1_Click()
    MsgBox Range("B7").Value - Range("B8").Value 
End Sub
 

greatcookies

Board Regular
Joined
Sep 25, 2005
Messages
187
Thanks! I did get it to work. How can I format the results to be only 2 decimal places?
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
Code:
Private Sub CommandButton1_Click() 
        MsgBox Format$(Range("B7").Value - Range("B8").Value, "0.00")
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,130,446
Messages
5,642,198
Members
417,259
Latest member
gtacw

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