VBA Code for countifs isnt working

Rant

New Member
Joined
Jul 3, 2021
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I am trying to get the macros to populate Cells(7, Column) with total hours worked by the contractor {sum value of number of (D x 12 +N x 12)} for each day from specified Start date to End date( based on Cells(5, Column) dates)

Would really appreciate if you could please help me on with this.

Thank you.



Workbook name: Roster-2021

Worksheet2 name: Summary

Cell ‘A7’ content is Contractor



Worksheet3 name: Roster

Range("D3:D200") has the following details: Permanent Employee, Temporary Employee and contractor

Range("F1:PA1") has dates from 01- January-2021 to 31-December-2021

Range("F4:PA200") has D and N. (D is Day shift and N is Night Shift)



The following code works fine:

Cells(5, Column) = StartD + Column - 3

Cells(6, Column) = Day(StartD + Column - 3)



Countifs code seems incorrect.





VBA Code:
VBA Code:
Private Sub CommandButton3_Click()

Application.ScreenUpdating = False



If CDate(TextBox2.Value) < CDate(TextBox1.Value) Then

MsgBox "Invalid entry! Start date must be < than End Date."

End If



Dim StartD As Date, EndD As Date

StartD = TextBox1.Value

EndD = TextBox2.Value

For Column = 3 To EndD - StartD + 3

Cells(5, Column) = StartD + Column - 3

Cells(6, Column) = Day(StartD + Column - 3)



Dim Con As String

Dim she2 As Worksheet

Dim she3 As Worksheet

Dim refD As Date



refD = StartD + Column - 3

Con = Range("A7").Text

she2 = Worksheets("Summary")

she3 = Worksheets("Roster")



she2.Cells(7, Column) = Application.Worksheet.CountIfs(she3!Range("D3:D200"), she2!Con, she3!Range("F1:PA1"), she2!refD, she3!Range("F4:PA200"), "D") + Application.Worksheet.CountIfs(she3!Range("D3:D200"), she2!Con, she3!Range("F1:PA1"), she2!refD, she3!Range("F4:PA200"), "N")



Next Column



Application.ScreenUpdating = True

End Sub
 
Last edited by a moderator:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Welcome to the MrExcel Message Board!

Since you're not telling us where you get stuck, we have to guess. Usually we are not successful in that. A quick look at your code yields the following.
Worksheets are in VBA terms Objects. Assigning an Object to a custom variable needs to be done using the Set statement.
So a code line like:
Rich (BB code):
she2 = Worksheets("Summary")
would be:
VBA Code:
Set she2 = Worksheets("Summary")

Hopefully this is of some help.
 
Upvote 0
I think we need to address the actual formula before doing too much else.
The initial requirement sounds like more like a sumifs than a countifs.
The formula itself has inconsistent range lengths and mixes vertical and horizontal ranges.
(all columns should be 3:200
and F1:PA1 will most likely need an Index/Match and a vertical criteria or is the the sum range)

Can we get an XL2BB of she3 & she2 and an example of the expected result ?
Step 1 is to get the formula working in Excel before incorporating it into VBA.

(I have reformatted the formula in the quote for clarity)
she2 = Worksheets("Summary")
she3 = Worksheets("Roster")

she2.Cells(7, Column) = Application.Worksheet.CountIfs(she3!Range("D3:D200"), she2!Con, _
she3!Range("F1:PA1"), she2!refD, _
she3!Range("F4:PA200"), "D") + _
Application.Worksheet.CountIfs(she3!Range("D3:D200"), she2!Con, _
she3!Range("F1:PA1"), she2!refD, _
she3!Range("F4:PA200"), "N")
 
Upvote 0
he2.Cells(7, Column) = Application.Worksheet.CountIfs(she3!Range("D3:D200"), she2!Con, she3!Range("F1:PA1"), she2!refD, she3!Range("F4:PA200"), "D") + Application.Worksheet.CountIfs(she3!Range("D3:D200"), she2!Con, she3!Range("F1:PA1"), she2!refD, she3!Range("F4:PA200"), "N")
Thank you GWteB and Alex for looking into this.

When I click the generate button, Summary!Cells(5, Column) gets populated with dates.

I'm getting stuck with the vba code (countifs) to count total hours worked by Non-working visitors-contractor {sum value of number of (D x 12 +N x 12)} for each date from specified Start date to End date (Cells(5, Column) when I click the generate button.

I am trying to get the macros to do countifs by:
finding Non-working visitors-contractor (C7 value) from "Roster!Range("D3:D200") and date (Cells(5, Column)) say 1st May from "Roster!Range("F1:PA1")"
count all the D's and N's from "Roster! Range("F4:PA200")" that match the two (C7 value- Non-working visitors-contractor, date in Summary!Cells(5, Column) dates)) values.
convert it to hours by D x 12 +N x 12.
 
Last edited by a moderator:
Upvote 0
Not providing XL2BB makes it really hard to provide assistance.
I assume the 2nd image is the Roster sheet.

The first step is to get a working formula just using Excel.
I was unable to get countifs to do what you want and ended up using Sumproduct.

See if you can get it working at your end before trying to incorporate it into VBA.
I have used Range Names which mimick the variables you are using in VBA.

20210706 Countif contiguous columns with VBA v02 SumProduct.xlsm
ABCDEFGHIJKLMN
1StartD3/05/20211/05/20212/05/20213/05/20214/05/20215/05/20216/05/20217/05/20218/05/20219/05/2021
2EndD5/05/2021Contractor Site PersonnelNDDNDDNDD
3Contractor Site PersonnelDNDNNDDND
4CountifsContractor Site PersonnelDDNDDNDDN
5N14Contractor Site PersonnelDNDDNDDND
6D23Contractor Site PersonnelNDDNDDNDD
7Total37Contractor Site PersonnelDND
8Contractor Site PersonnelDDN
9Contractor Site PersonnelNDD
10Other Criteria (Con)Contractor Site PersonnelOtherNDN
11OtherDND
12Contractor Site PersonnelXND
13Contractor Site PersonnelXDN
14Contractor Site PersonnelNDD
15Contractor Site PersonnelDDN
16Contractor Site PersonnelDND
Roster
Cell Formulas
RangeFormula
B5:B6B5=SUMPRODUCT(($D$1:$D$204=Con)*(INDEX($F$1:$PA$204,0,MATCH(StartD,$F$1:$PA$1,0)):INDEX($F$1:$PA$204,0,MATCH(EndD,$F$1:$PA$1,0))=$A5))
B7B7=SUM(B5:B6)
Named Ranges
NameRefers ToCells
Con=Roster!$B$10B5:B6
EndD=Roster!$B$2B5:B6
StartD=Roster!$B$1B5:B6
 
Upvote 0
Awesome. That works. Thanks mate.

However, when I add this to vba, I get Run-time error 438! "Object doesn't support this property or method"

"Summary" sheet Code:

Dim Ros As Worksheet

Set Ros = Worksheets("Roster")


Cells(7, Column) = WorksheetFunction.SumProduct((Ros!Range(D1, D154) = A7) * (WorksheetFunction.Index(Ros!Range(F1, PA154), 0, WorksheetFunction.Match(Cells(5, Column), Ros!Range(F1, PA1), 0)) = "D")) + WorksheetFunction.SumProduct((Ros!Range(D1, D154) = A7) * (WorksheetFunction.Index(Ros!Range(F1, PA154), 0, WorksheetFunction.Match(Cells(5, Column), Ros!Range(F1, PA1), 0)) = "N"))
 
Upvote 0
Do you just want the result of the formula in Cells(7, Column) or the formula itself ?
Although the current syntax is indicates the result, I suspect you want the formula to appear in the cell.
 
Upvote 0
Sorry I couldn't get the countifs to work initially because I mistakenly assumed you wanted to add the whole date range in the one formula.
Since I think you want to put the formula in each cell, for each day, I have gone back to it since it should be faster than sumproduct.
I might have made it bit more complicated than it needs to be but see how you go with it.

Before the For statement my formula needs this.

VBA Code:
        Set she2 = Worksheets("Summary")
        Set Ros = Worksheets("Roster")
        
        Dim LastRowNo As Long
        Dim sLastCol As String
              
        LastRowNo = 154
        sLastCol = "PA"

Inside the Loop this
VBA Code:
        she2.Cells(7, Column).Formula = "=COUNTIFS(Roster!$D$1:$D$" & LastRowNo & ",$A7," & _
                                           "INDEX(Roster!$F$1:$" & sLastCol & "$" & LastRowNo & _
                                             ",0,MATCH(" & she2.Cells(5, Column).Address(True, False) & ",Roster!$F$1:$" & sLastCol & "$1,0)),""D"")" & _
                                        "+COUNTIFS(Roster!$D$1:$D$" & LastRowNo & ",$A7," & _
                                           "INDEX(Roster!$F$1:$" & sLastCol & "$" & LastRowNo & _
                                             ",0,MATCH(" & she2.Cells(5, Column).Address(True, False) & ",Roster!$F$1:$" & sLastCol & "$1,0)),""N"")"
 
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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