simple subtraction of one row from another in vba

doogis

New Member
Joined
Feb 7, 2016
Messages
27
Hello,

Is there an easy way to subtract one row from another row but only if there is data. For example a2-a3 to z2-z3 etc. Only if those fields are populated.

Thanks,
Greg
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Logic: If A2 and A3 is not blank, then subtract A2 minus A3.
Code:
Sub myMacro()
     Dim myArray(1, 25) As Long
     myArray(0, 0) = Range("A2").Value
     myArray(1, 0) = Range("A3").Value
     myArray(0, 1) = Range("B2").Value
     myArray(1, 1) = Range("B3").Value
     myArray(0, 2) = Range("C2").Value
     myArray(1, 2) = Range("C3").Value
     myArray(0, 3) = Range("D2").Value
     myArray(1, 3) = Range("D3").Value
     'Continue here and add all the ranges to the array.
     i = 0
     Do Until i > 25
          value1 = myArray(0, i)
          value2 = myArray(1, i)
          myResult = myFunction(value1, value2)
          MsgBox myResult
          i = i + 1
     Loop
End Sub
Function myFunction(value1, value2)
     If value1 <> "" _
     And value2 <> "" Then
          myFunction = value1 - value2
          Exit Function
     End If
     myFunction = ""
End Function
 
Upvote 0
Hello,

Thanks for the reply. Is there something I can do with a "range" command? If my data is not static and is >25 then I have to change the code.
My data looks like this,

Channel 1
Channel 2Channel 3
Channel 4Channel 5
100
200300400500
20.2
20.220.2
20.220.2

<colgroup><col span="5"></colgroup><tbody>
</tbody>
All I want to do is subtract C3 from C2 ....T3 from T2 etc.

Thanks,
Greg
 
Upvote 0
Code:
Sub myMacro()
     c = 1
     lc = Cells(1, Columns.Count).End(xlToLeft).Column
     Do Until c > lc
          Cells(3, c).Value = myFunction(Cells(2, c).Value, Cells(3, c).Value)
          c = c + 1
     Loop
End Sub
Function myFunction(value1, value2)
     If value1 <> "" _
     And value2 <> "" Then
          myFunction = value1 - value2
          Exit Function
     End If
     myFunction = ""
End Function
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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