Use CountIFS in VBA with variable criteria

PROACT

New Member
Joined
Sep 27, 2016
Messages
7
Hi I've been trying this for a decade (it seems like that) and searched the whole web, but probably it is just some dots, slashes and other punctual stuff thats wrong with my code.

Ok what I'm trying to do is rather easy, I would like to put a countifs formula in a cell by VBA.
The more difficult part is, the source data is in another workbook, and the criteria must variable.

So in the example the variables are:
  • Project
  • Year
  • Month

I've wel declared them as integers, the value of the variables are linked. But I can't get that COUNTIFS-formula working.

Can anyone please help me out?
Thanks in advance...

Code:
ActiveCell.Formula = _            "=COUNTIFS('[Masterfile contacten_RAW_Data.xlsx]Data'!R2C11:R99999C11,ProjectId,'[Masterfile contacten_RAW_Data.xlsx]Data'!R2C18:R99999C18,Year,'[Masterfile contacten_RAW_Data.xlsx]Data'!R2C19:R99999C19,Month"
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try like this:

Code:
ActiveCell.Formula = _
    "=COUNTIFS('[Masterfile contacten_RAW_Data.xlsx]Data'!R2C11:R99999C11," & ProjectId & ",'[Masterfile contacten_RAW_Data.xlsx]Data'!R2C18:R99999C18," & myYear & ",'[Masterfile contacten_RAW_Data.xlsx]Data'!R2C19:R99999C19," & myMonth & ")"
 
Upvote 0
Oh I changed month and year to myMonth and myYear because they could be confused with genuine excel functions.
 
Upvote 0
Try...

Code:
ActiveCell.FormulaR1C1 = "=COUNTIFS('[Masterfile contacten_RAW_Data.xlsx]Data'!R2C11:R99999C11," & ProjectId & ",'[Masterfile contacten_RAW_Data.xlsx]Data'!R2C18:R99999C18," & Year & ",'[Masterfile contacten_RAW_Data.xlsx]Data'!R2C19:R99999C19," & Month & ")"

Also, since COUNTIFS is not an array formula, using whole column references shouldn't adversely affect the efficiency of the formula...

Code:
ActiveCell.FormulaR1C1 = "=COUNTIFS('[Masterfile contacten_RAW_Data.xlsx]Data'!C11:C11," & ProjectId & ",'[Masterfile contacten_RAW_Data.xlsx]Data'!C18:C18," & Year & ",'[Masterfile contacten_RAW_Data.xlsx]Data'!C19:C19," & Month & ")"

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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