VBA code that count last nth occurrences in data range

chefich

New Member
Joined
Sep 18, 2017
Messages
3
Hello All,


I need VBA code that counts last four occurrences in the data range.

Example: Part 4

Col 1Col 2
Part 1RResult / Last 4 occurrences
Part 2DRDL
Part 3LPart 4211
Part 4L
Part 5R
Part 2D
Part 4R
Part 1R
Part 3L
Part 5R
Part 2R
Part 3L
Part 5R
Part 2L
Part 1L
Part 4R
Part 4D
Part 1R
Part 2D
Part 3L
Part 4L
Part 5R
Part 2D
Part 4R
Part 1R
Part 3L
Part 5R
Part 2R

<tbody>
</tbody>


Please help!

Kind Regards
Stefan
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the forum.

You can do this with a formula:

ABCDEF
1Col 1Col 2
2Part 1RResult / Last 4 occurrences
3Part 2DRDL
4Part 3LPart 4211
5Part 4L
6Part 5R
7Part 2D
8Part 4R
9Part 1R
10Part 3L
11Part 5R
12Part 2R
13Part 3L
14Part 5R
15Part 2L
16Part 1L
17Part 4R
18Part 4D
19Part 1R
20Part 2D
21Part 3L
22Part 4L
23Part 5R
24Part 2D
25Part 4R
26Part 1R
27Part 3L
28Part 5R
29Part 2R

<tbody>
</tbody>
Sheet3

Array Formulas
CellFormula
D4{=SUM(IF($A$2:$A$50=$C$4,IF($B$2:$B$50=D$3,IF(ROW($A$2:$A$50)>=LARGE(IF($A$2:$A$50=$C$4,ROW($A$2:$A$50)),4),1))))}

<tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>




Or do you have a specific reason to want VBA?
 
Last edited:
Upvote 0
Hello Eric,

Thank you for your answer.
I need VBA code cause it's part of my project and I am stuck on this step.

Best Regards
Stefan
 
Upvote 0
In Excel, the line between formulas and VBA is sometimes very blurry. For example, the following macro does what you ask:

Code:
Sub GetLast4()


    [d4] = Evaluate("SUM(IF($A$2:$A$50=$C$4,IF($B$2:$B$50=D$3,IF(ROW($A$2:$A$50)>=LARGE(IF($A$2:$A$50=$C$4,ROW($A$2:$A$50)),4),1))))")
    [e4] = Evaluate("SUM(IF($A$2:$A$50=$C$4,IF($B$2:$B$50=E$3,IF(ROW($A$2:$A$50)>=LARGE(IF($A$2:$A$50=$C$4,ROW($A$2:$A$50)),4),1))))")
    [f4] = Evaluate("SUM(IF($A$2:$A$50=$C$4,IF($B$2:$B$50=F$3,IF(ROW($A$2:$A$50)>=LARGE(IF($A$2:$A$50=$C$4,ROW($A$2:$A$50)),4),1))))")
End Sub
but as you can see, it's just the same formulas in VBA.

When you say "part of my project", do you mean a school project? Generally speaking, we don't like doing people's homework. It robs the student of the opportunity to learn. We may give some pointers however. If this is a personal or work project, we're a bit more lenient.

In any case, if I were to write a more "program-like" macro, I'd start with this line:

Code:
lr = Cells(Rows.Count, "A").End(xlUp).Row

which gets the last row of your data. Then I'd set up 4 counters, 1 for the part number, 3 for R, D and L. Then I'd create a loop going backwards:

Code:
For r = lr to 2 Step -1

And check each cell in your column until you get your limit. If you have a basic knowledge of VBA, that should be enough to get you going.

Good luck!
 
Upvote 0
Hi Eric,

Thank you for your help!
It's not for a school project. I am 42 years old :rolleyes: with very very limited VBA knowledge.
I will try this tomorrow.
Nave a nice evening
Cheers,
Stefan

 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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