VBA WorksheetFunction - countifs using month not working.

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
196
Hi

I have this code which isn't working;
VBA Code:
For Each c In Sheets("Support").Range("B2:B13")

c.Value = (Application.WorksheetFunction.Sum(Application.CountIfs(Worksheets("Data").Range("G:G"), Array("6", "7"), Month(Worksheets("Data").Range("B:B")), Month(c.Offset(, -1).Value))) - Application.WorksheetFunction.Sum(Application.CountIfs(Worksheets("Data").Range("G:G"), Array("1", "2", "3"), Month(Worksheets("Data").Range("B:B")), Month(c.Offset(, -1).Value)))) / Application.WorksheetFunction.Sum(Application.CountIfs(Worksheets("Data").Range("G:G"), Array("1", "2", "3", "4", "5", "6", "7"), Month(Worksheets("Data").Range("B:B")), Month(c.Offset(, -1).Value)))

Next

In short, it's the parts which are within the Month parenthesis which provide a run-time error '13':type mismatch... (removing it entirely then runs fine but isn't the result I want). And for some reason, I just can't get my head around it.

In Sheets("Support").Range("B1:B12") - this contains the 1st of each month in a dd/mm/yyyy format.
In Sheets("Data").Range("B:B") - this contains random dates of the month, across multiple months, also in a dd/mm/yyyy format.

As part of the above worksheet function, all I'm trying to do is include the parameter to 'countif', if the month within each range is the same... What am I missing? Any help or suggestions would be hugely appreciated.

Thank you
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try using the Evauate method instead. In the following example...

  1. The code refers to the active workbook. However, if the workbook running the code contains your target worksheets, replace each instance of ActiveWorkbook with ThisWorkbook.
  2. The code calculates 3 separate counts before performing the final calculation. However, I've done the first count, you'll need to do the same for the other 2 counts.
  3. The Evaluate method has a 255 character limit. Exceeding this limit will cause an error.

VBA Code:
    With ActiveWorkbook.Worksheets("Data")
   
        Dim LastRow As Long
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

        Dim RangeB As Range
        Set RangeB = .Range("B1:B" & LastRow)
       
        Dim RangeG As Range
        Set RangeG = .Range("G1:G" & LastRow)
       
    End With
   
   
    Dim c As Range
    Dim count1 As Long
    Dim count2 As Long
    Dim count3 As Long
   
    For Each c In ActiveWorkbook.Sheets("Support").Range("B2:B13")
   
        count1 = Evaluate("SUM(IF(ISNUMBER(MATCH(" & RangeG.Address(, , , True) & ",{6,7},0)),IF(MONTH(" & RangeB.Address(, , , True) & ")=MONTH(" & c.Offset(, -1).Value & "),1)))")
        count2 = Evaluate(" . . . ")
        count3 = Evaluate(" . . . ")
       
        c.Value = (count1 - count2) / count3
       
    Next

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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