Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA Countifs not Working

Posted on 2014-10-03
7
Medium Priority
?
233 Views
Last Modified: 2014-10-04
The following code just does not seem to work, it probable has to do with date syntax but it baffles me.

Sub myCountBBO2()

Dim counts As Integer, i As Integer
Dim LastRow As Long
Dim vArray1 As Variant
Dim dt1 As Date, dt2 As Date
Dim cnd1 As String, cnd2 As String, cnd3 As String

LastRow = Sheets(1).Cells(Sheets(1).Rows.Count, "B").End(xlUp).Row
vArray1 = WorksheetFunction.Transpose(WorksheetFunction.Transpose(Sheets(1).Range("B7").Resize(LastRow, 8).Value))

cnd1 = Sheets(1).Range("F2").Value
cnd2 = Sheets(1).Range("G2").Value
cnd3 = Sheets(1).Range("H2").Value
dt1 = Format(Sheets(1).Range("L2").Value, "d-mmm-yy")
dt1 = Format(Sheets(1).Range("L3").Value, "d-mmm-yy")

MsgBox dt1

For i = LBound(vArray1) To UBound(vArray1)
    If vArray1(i, 1) = cnd1 And vArray1(i, 2) = cnd2 And vArray1(i, 3) = CInt(cnd3) And vArray1(i, 4) >= dt1 Or vArray1(i, 4) <= dt2 Then
        counts = counts + 1
    End If
Next i

Sheets(1).Range("M2") = counts

End Sub
Trial-Count.xlsm
0
Comment
Question by:DougDodge
  • 3
  • 2
  • 2
7 Comments
 
LVL 27

Expert Comment

by:ProfessorJimJam
ID: 40360594
0
 

Author Comment

by:DougDodge
ID: 40360782
No luck, it is exactly the same, nothing has changed..... In fact it is the same file I posted....
0
 
LVL 27

Expert Comment

by:ProfessorJimJam
ID: 40360947
Can you upload a sample file that indicates a correct result. So that based on that u can see what is required
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 15

Expert Comment

by:ChloesDad
ID: 40360978
I think that your logic is wrong

For i = LBound(vArray1) To UBound(vArray1)
     If vArray1(i, 1) = cnd1 And vArray1(i, 2) = cnd2 And vArray1(i, 3) = CInt(cnd3) And vArray1(i, 4) >= dt1 Or vArray1(i, 4) <= dt2 Then
         counts = counts + 1
     End If
 Next I

do you actually mean

For i = LBound(vArray1) To UBound(vArray1)
     If vArray1(i, 1) = cnd1 And vArray1(i, 2) = cnd2 And vArray1(i, 3) = CInt(cnd3) And (vArray1(i, 4) >= dt1 Or vArray1(i, 4) <= dt2) Then
         counts = counts + 1
     End If
 Next i

Without the brackets if the last bit is true then the expression will be true irrespective of the first parts. With the brackets the first 3 must be true and one of the last two must be true for the expression to be true.
0
 

Author Comment

by:DougDodge
ID: 40361198
Sorry, it still gives the wrong answer. This is really a confusing problem....
I have attached the same file, if you open it you should see the filtered results of a count of 3, and yet the code gives a count of 0 in cell $M$2.
Maybe an array is not the right approach to this.
Test-of-Countif.xlsm
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 2000 total points
ID: 40361221
There are 2 mistaked

firstly all the conditions should be AND

to check for a date between two dates you need A>= X AND A <= Y

the second is that you are setting dt1 twice, the second should be dt2

cnd1 = Sheets(1).Range("F2").Value
cnd2 = Sheets(1).Range("G2").Value
cnd3 = Sheets(1).Range("H2").Value
dt1 = Format(Sheets(1).Range("L2").Value, "d-mmm-yy")
dt2 = Format(Sheets(1).Range("L3").Value, "d-mmm-yy")



For i = LBound(vArray1) To UBound(vArray1)
    If vArray1(i, 1) = cnd1 And vArray1(i, 2) = cnd2 And vArray1(i, 3) = CInt(cnd3) And vArray1(i, 4) >= dt1 And vArray1(i, 4) <= dt2 Then
        counts = counts + 1
    End If
Next i

Sheets(1).Range("M2") = counts
0
 

Author Closing Comment

by:DougDodge
ID: 40361247
You were right, I should have caught that. I was changing so many things trying to get it to work I totally missed it.... Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Loops Section Overview

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question