VBA Calculations between ranges

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,749
Office Version
  1. 365
Platform
  1. Windows
What's the best way to do calculations between same sized ranges in VBA? I've got several ranges that have to use the same kind of calculations between them :

Take the first cell value from one range and add/minus/multiply it with the first cell value in another range and put the value in the first cell of a third range. Go to second cells in both ranges and do the same calculations between them and place the value in the second cell in the third range and so on 'till the last cell of both ranges has been calculated.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Several ways to do this. On the worksheet itself, you can copy the first range of cells to the third range, then copy the second range of cells and EDIT> PASTESPECIAL > Add/Subtract/Multiply it over the top of the values in the third range.

This would be the simplest way to do the SAME math on those two ranges. You can record yourself doing this to get base code to get you started.

You could also "Loop" through the rows and do the math one row at a time...much less elegant, but would still work.
 
Upvote 0
You could try this...

Code:
Sub Calc_Range()
'Assumes all three ranges are the same size...
Dim inp_rng1 As Range, inp_rng2 As Range, out_rng As Range
Dim c As Range, r As Range
Dim i As Integer, j As Integer
    Set inp_rng1 = Worksheets(1).Range("Rng_One")
    Set inp_rng2 = Worksheets(1).Range("Rng_Two")
    Set out_rng = Worksheets(1).Range("Rng_Three")
    
    i = 0: j = 0
    For i = 1 To inp_rng1.Rows.Count
        For j = 1 To inp_rng1.Columns.Count
            Debug.Print inp_rng1(i, j)
            out_rng(i, j) = inp_rng1(i, j) + inp_rng2(i, j)
        Next j
    Next i
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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