Add the values in 3 cells, subtract the value in a fourth and put the result and the fifth cell

CathalB

New Member
Joined
Mar 12, 2018
Messages
5
Hi Everyone,
Hope you're all in good form.

I am stuck on this. I have a worksheet containing lots of columns 300+ and data.
I am transforming this into a new worksheet with the columns re-arranged and some renamed etc.
The VB code I have so far has worked to complete these tasks OK.

However, now I need to do the above i.e. Add the values in 3 cells, subtract the value in a fourth and put the result and the fifth cell.
This is what my current code looks like - which is obviously not working

Sub CalcTotalMisusRisk()
Dim i As Long
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
Worksheets("Sheet2").Range("DD" & i) = ("CZ2" & i & ",DA2" & i & ",DB2" & i) - (",DC2" & i)
Next i​
End Sub

The values in cells CZ2, DA2 and DB2 are just numbers and row 1 contains the headers hence <for i="2" to="" cells="" etc="">
but VB is kicking up the following error:
"Runtime error '13'
Type mismatch

Apologies if I haven't articulated the problem very well - I'm (as you can see) not a coder.</for>
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Code:
Worksheets("Sheet2").Range("DD" & i).Value = Range("CZ2" & i).Value + Range("DA2" & i).Value + Range("DB2" & i).Value - Range("DC2" & i).Value
 
Upvote 0
Hi CathalB.

Based on what you have described, you need something like :
Code:
[COLOR=#333333]Worksheets("Sheet2").Range("DD" & i).Value = Range("CZ" & i).Value + Range("DA" & i).Value  + Range("DB" & i).Value - Range("DC" & i).Value[/COLOR]

But seeing as you are putting this new value in the column after the 4 cells in question, why use VBA at all ?

You could just put a formula in Cell DD2
Code:
=CZ2+DA2+DB2-DC2
..and double-click in the bottom right corner of Cell DD2 to populate the formula down the column (or you could drag it down yourself).

Seems like you might be using a sledgehammer to crack a walnut ! :)

Cheers,
Warren K.
 
Upvote 0
Code:
Worksheets("Sheet2").Range("DD" & i).Value = Range("CZ2" & i).Value + Range("DA2" & i).Value + Range("DB2" & i).Value - Range("DC2" & i).Value

Still did not work correctly for me hope you are a little closer.
 
Upvote 0
Hi CathalB.

Based on what you have described, you need something like :
Code:
[COLOR=#333333]Worksheets("Sheet2").Range("DD" & i).Value = Range("CZ" & i).Value + Range("DA" & i).Value  + Range("DB" & i).Value - Range("DC" & i).Value[/COLOR]

But seeing as you are putting this new value in the column after the 4 cells in question, why use VBA at all ?

You could just put a formula in Cell DD2
Code:
=CZ2+DA2+DB2-DC2
..and double-click in the bottom right corner of Cell DD2 to populate the formula down the column (or you could drag it down yourself).

Seems like you might be using a sledgehammer to crack a walnut ! :)

Cheers,
Warren K.

Hi Warren,

Thanks so much for helping me out, I appreciate it.
I admit it does seem like a lot just to do a little. However, I am preparing for a phase of a national project where I'm going to be sent an awful lot of data from all over the country (I'm in Ireland by the way!) and I need to automate as much of the work to transform these excel files into the format and structure I need to before importing them into SPSS and analyse them.
Hence, this small piece of code is part of a module of code I'm building up in the VB editor to perform the transformations.

Had I known how much I would be relying on Excel and VB I'd have done a course beforehand! ;)

Thanks again!
 
Upvote 0
Hi Drrellik and WarrenK,

Between both of you my issue is now sorted and the code is doing exactly what I need.
Thanks so much for taking the time to help me out, I really appreciate it.

Have a great day,
Charles.
 
Upvote 0
Thanks CathalB and thanks WarrenK I missed the "2"

Thanks again for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,662
Members
449,462
Latest member
Chislobog

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