Adjust value to cells in a row if the value in a different row is 0

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I've got a simple table of data

Just 4 columns and 3 rows (see the table below).

Cells A1, B1, C1 and D1 have the values 1, 2, 3, and 4, respectively.

Cells A2, B2, C2 and D2 have the values 1, 2, 3 and 2, respectively.

Cells A3, B3, C3 and D3 have the values 0, 1, 0 and 2, respectively.

Is there code that will add the value in row 3 to the value in row 1 IF the value in row 3 is NOT zero?

So in the example below, the value in cell B1 would become 3 ie 2 (in cell B1)+ 1(in cell B3).

And cell D1 would become 6 ie 4 in cell D1 + 2 in cell D3.




1​
2​
3​
4​
1​
1​
3​
2​
0​
1​
0​
2​
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Is there a formula in the first row or are you manually entering information into it?
 
Upvote 0
Good question. The information is entered manually....
 
Upvote 0
VBA Code:
Sub Math()
Dim Number_1 As Integer
Dim Number_2 As Integer
Dim Number_3 As Integer
Dim Number_4 As Integer
Dim Number_5 As Integer
Dim Number_6 As Integer
Dim Number_7 As Integer
Dim Number_8 As Integer

 Number_1 = Range("A1").Value
 Number_2 = Range("A3").Value
 Number_3 = Range("B1").Value
 Number_4 = Range("B3").Value
 Number_5 = Range("C1").Value
 Number_6 = Range("C3").Value
 Number_7 = Range("D1").Value
 Number_8 = Range("D3").Value
 
If Number_2 > 0 Then Range("A1").Value = Number_1 + Number_2
If Number_4 > 0 Then Range("B1").Value = Number_3 + Number_4
If Number_6 > 0 Then Range("C1").Value = Number_5 + Number_6
If Number_8 > 0 Then Range("D1").Value = Number_7 + Number_8

End Sub
 
Upvote 0
Does this also do what you want? Test with a copy of your data.

VBA Code:
Sub Add_Em()
  Range("A3:D3").Copy
  Range("A1:D1").PasteSpecial Operation:=xlAdd
  Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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