How do I create a macro from a SUMPRODUCT formula for a range whose number of rows changes?

bananius

New Member
Joined
Aug 27, 2014
Messages
8
I have a SUMPRODUCT formula which can be written as either:
=SUMPRODUCT(0+(COUNTIF(OFFSET(AL2:AT2,ROW(AL2:AT?)-MIN(ROW(AL2:AT?)),,),"VAC")<5))
OR
=SUMPRODUCT(--(MMULT(--(AL2:AT?="Vac"),{1;1;1;1;1;1;1;1;1})<5))
However, I have to apply this formula to different sheets. The column range always stays the same (AL:AT) from sheet to sheet, but the number of rows changes per sheet. I'm starting the column range at AL2 to exclude the header.
I want to create a macro for the formula so that I can assign it to a command button so that it will count how many rows in the AL:AT range have less than five instances of the string "VAC" in it. And so the count pops up in a message box when you press the command button.
If anyone can provide any help that would be wonderful. Thank you in advance!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
This code will work

Although it uses a loop Calculated from the activesheet and does a CountIf across the columns

Code:
Sub VacAlert()
Dim Boo As Variant
Dim MyDepth As Long
Dim doo As Long

    MyDepth = ActiveSheet.Cells(Rows.Count, "AL").End(xlUp).Row
    
    doo = 0
    For x = 2 To MyDepth
    
        Boo = Application.WorksheetFunction.CountIf(Range("AL" & x & ":AT" & x), "VAC")
        If Boo < 5 Then
          doo = doo + 1
        End If
    
    Next x
    
    MsgBox "Hey theres " & doo
End Sub
 
Upvote 0
This code will work

Although it uses a loop Calculated from the activesheet and does a CountIf across the columns

Code:
Sub VacAlert()
Dim Boo As Variant
Dim MyDepth As Long
Dim doo As Long

    MyDepth = ActiveSheet.Cells(Rows.Count, "AL").End(xlUp).Row
    
    doo = 0
    For x = 2 To MyDepth
    
        Boo = Application.WorksheetFunction.CountIf(Range("AL" & x & ":AT" & x), "VAC")
        If Boo < 5 Then
          doo = doo + 1
        End If
    
    Next x
    
    MsgBox "Hey theres " & doo
End Sub

Thank you for your response CharlesChuckieCharles! However, when I put in the code it gave me a result of 77 rows when there are only 41! When I just did the MyDepth calculation it gave me 85 rows so I think that's where the problem lies. I also tried to do the ActiveSheet.Cells(Rows.Count... function but it was not returning the right results...

This is what the test range looks like which is from columns AB:AL

0654a5_814a96470f9344fcae34d2d7b9f7efae.png_srz_p_658_661_75_22_0.50_1.20_0.00_png_srz
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,571
Members
453,054
Latest member
arz007

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