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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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
 
Upvote 0
Code:
Sub SubtractTwoCells()
    MsgBox Range("B7").Value - Range("B8").Value
End Sub
 
Upvote 0
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!
 
Upvote 0
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
 
Upvote 0
Code:
Private Sub CommandButton1_Click() 
        MsgBox Format$(Range("B7").Value - Range("B8").Value, "0.00")
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,556
Messages
6,114,284
Members
448,562
Latest member
Flashbond

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
Back
Top