Type Mismatch error when subtracting two cells in VBA

AC1982

New Member
Joined
Jul 9, 2009
Messages
40
Hi,

I am trying to subtract and add a range of cells in VBA, but I keep getting a run-time error 13 Type mismatch error. Can anyone help, here is my code -

Code:
ActiveSheet.Range("G55").Value = Range("G56").Value - WorksheetFunction.Sum(Range("G56"), Range("G20"), Range("G31"), Range("G40"), Range("G49"), Range("G53:G54"))

I want the value of G55 to be G56-SUM(G20,G31,G40,G49,G53,G54), I am not sure how to fix this.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try
Code:
Range("G55").Value = Val(Range("G56").Value - Application.SUM(Range("G20,G31,G40,G49,G53,G54)))
 
Upvote 0
Try
Code:
Range("G55").Value = Val(Range("G56").Value - Application.SUM(Range("G20,G31,G40,G49,G53,G54)))

Hi Mike,

Thanks for your help.

When I type the code below in, I still get the same error.

Code:
Range("G55").Value = Val(Range("G56").Value - _
Application.Sum(Range("G20, G31, G40, G49, G53, G54")))
 
Upvote 0
Correct parenthetic placement would help both of us
Rich (BB code):
Range("G55").Value = Val(Range("G56").Value) - Application.SUM(Range("G20,G31,G40,G49,G53,G54"))
 
Upvote 0
Thanks works great.

I am trying to divide two cells by eachother and I also get the same error (type mismatch) -

Code:
Range("H55") = Range("G55").Value / Range("G56").Value

I am not sure why this is happening and how I can avoid it in the future.
 
Upvote 0
Looks like Mike may be Offline, so

Following the same concept as your subtracting:

Rich (BB code):
Range("H55") = Val(Range("G55").Value / Val(Range("G56"))
 
Upvote 0
Yes, but to get the formula absolutley bullet proof, I'd use CStr to coerce any error value to a string and then Val to get a number

Code:
Range("H55") = Val(CStr(Range("G55").Value)) / Val(CStr(Range("G56")))

But that would error if G56 doesn't hold a non-zero number (div by 0)

It might be better to use Excel's built in error handling so that the VB routine doesn't crash

Code:
Range("H55").FormulaR1C1 = "=RC[-1]/R[1]C[-1]"
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

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