Looping calculations for blocks of data

Atvolpini

New Member
Joined
Oct 26, 2017
Messages
15
Hi,

I have recorded a macro that is performing a couple of simple calculations on a dataset that is 12 columns wide by 8 rows tall. However, I now need to to have the macro loop those same calculations over each 12x8 block, until a blank row is reached. The data is very regular, every set is the same size and the next set occurs directly after. If desired Id be happy to upload the code I have. Here is an example of the data:

0.0000.2680.2600.3850.2050.438-0.0010.3810.2220.7320.495-0.002
-0.0010.3270.3570.3340.7480.243-0.0020.3510.2880.3890.0280.271High Growth15
0.7830.0000.7670.0000.2270.3680.3810.7650.1530.4250.4230.306Med Growth51
0.3030.2370.3500.0000.4660.4980.0990.3200.0010.7740.7800.307Low Growth8
0.0650.7380.340-0.0010.1540.3520.7790.7200.4490.746-0.0020.023No Growth22
-0.0020.1910.3460.0870.7870.2570.3170.001-0.0010.3180.6910.232Sum96
0.687-0.0020.5440.0520.3440.7520.1130.6910.470-0.0020.481-0.002
0.0000.3400.2520.4110.2930.2380.3710.2670.7120.705-0.0020.003
0.0010.3210.2740.4240.2501.4890.6000.8010.2960.0560.0080.000
0.3180.1800.0870.3060.0100.3290.3230.2880.3350.2630.0110.217
0.2410.1540.3220.1830.7330.0010.3210.7260.2130.3080.0050.299
0.7440.2630.3440.4820.5140.2760.2860.2780.2290.3700.3890.780
0.0010.3900.2450.1860.0700.0100.2980.2710.2910.3520.0000.646
0.2220.1440.3350.0000.3480.2480.7080.4500.3440.3010.0960.007
0.2810.3310.3480.6870.3300.3010.0790.3810.4300.3080.1850.007
0.0000.5920.6770.3220.2880.1130.3570.2630.2390.1980.736-0.001

<colgroup><col span="12"><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Record your macro with relative references and start and finish on the same relative cell in the first 2 datasets. Then have another macro which loops through the datasets, calling the recorded macro on each dataset.

For example, I assumed your data starts in A1. So I selected A1, started the macro recorder with relative references, created formulas for the first dataset, (e.g. in M1 I entered the formula =SUM(A1:L1)), then selected the first cell of the next dataset (e.g. A10) and stopped the recorder. This produced Macro1 below and I wrote the All_DataSets macro to loop through all the datasets.
Code:
Sub All_DataSets()
    Dim lastRow As Long, r As Long
    lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    Range("A1").Select
    For r = 1 To lastRow Step 10
        Macro1
    Next
End Sub

Sub Macro1()
    ActiveCell.Offset(0, 12).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=SUM(RC[-12]:RC[-1])"
    ActiveCell.Offset(9, -12).Range("A1").Select
End Sub
 
Last edited:
Upvote 0
I tried to use the code you provided as a base, but it seems to only process using one formula, while I have multiple. I tried to add in more, but that causes the macro to only process the first data block. I have very little VBA knowledge so I might just be misunderstanding what you mean.

Code:
Sub Formulas()
    ActiveCell.Offset(0, 12).Range("A1").Select
        Range("Q2").Select
        ActiveCell.FormulaR1C1 = "=COUNTIF(R[-1]C[-14]:R[6]C[-3], "">=0.7"")"
        Range("Q3").Select
        ActiveCell.FormulaR1C1 = _
            "=COUNTIFS((R[-2]C[-14]:R[5]C[-3]),(""<0.7""),(R[-2]C[-14]:R[5]C[-3]),("">=0.2""))"
        Range("Q4").Select
        ActiveCell.FormulaR1C1 = _
            "=COUNTIFS((R[-3]C[-14]:R[4]C[-3]),(""<0.2""),(R[-3]C[-14]:R[4]C[-3]),("">=0.05""))"
        Range("Q5").Select
        ActiveCell.FormulaR1C1 = "=COUNTIF(R[-4]C[-14]:R[3]C[-3], ""<0.05"")"
        Range("Q6").Select
        ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    ActiveCell.Offset(8, -14).Range("A1").Select
End Sub
 
Upvote 0
Yes, my recorded macro only has one formula, just for demonstration purposes. Follow my steps carefully and record your macro with all your formulas, or just 2 or 3 to start with. Make sure the macro recorder is using relative references, not the default absolute references.

What are your formulas, exactly? It's hard to read the R1C1 formulas in your code.

Which cell does the data start in?
 
Last edited:
Upvote 0
I have recorded a macro that is performing a couple of simple calculations on a dataset that is 12 columns wide by 8 rows tall.
It is hard to give you a code approach without know what those "simple calculations" are... care to describe them for us?
 
Upvote 0
What is the last column with data? & does that column have values, or formulae?
 
Upvote 0
Thank you all for the help, i apologize for any confusion. Between John_w's recommendations and a little tampering I got the macro running successfully. Here is what ended up working. Just for completeness, Rick Rothstein, the calculations in their general form are also included below.

High Growth=COUNTIF(C1:N8, ">0.7")
Med Growth=COUNTIFS((C1:N8),("<0.7"),(C1:N8),(">0.2"))
Low Growth=COUNTIFS((C1:N8),("<0.2"),(C1:N8),(">0.05"))
No Growth=COUNTIF(C1:N8, "<0.05")
Sum=SUM(B1:B4)

<tbody>
</tbody>

Code:
Code:
Sub Relative_OD_Loop()
' Keyboard Shortcut: Ctrl+Shift+I
    Application.ScreenUpdating = False
    Range("A1").Select
    Do Until IsEmpty(ActiveCell)
       Relative_OD
    Loop
End Sub
____________


Sub Relative_OD()
'
' Relative_OD Macro
'


    ActiveCell.Offset(1, 14).Range("A1").Select
    ActiveCell.FormulaR1C1 = "High Growth"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Med Growth"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Low Groth"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = ""
    ActiveCell.Offset(-1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Low Growth"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "No Growth"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Sum"
    ActiveCell.Offset(-4, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(R[-1]C[-13]:R[6]C[-2],"">=0.7"")"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIFS((R[-2]C[-13]:R[5]C[-2]),(""<0.7""),(R[-2]C[-13]:R[5]C[-2]),("">=0.2""))"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIFS((R[-3]C[-13]:R[4]C[-2]),(""<0.2""),(R[-3]C[-13]:R[4]C[-2]),("">=0.05""))"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(R[-4]C[-13]:R[3]C[-2],""<0.05"")"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    ActiveCell.Offset(3, -15).Range("A1").Select
End Sub

 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,405
Members
449,448
Latest member
Andrew Slatter

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