Phil Morris
New Member
- Joined
- Oct 9, 2011
- Messages
- 17
Hello all,
I am suffering from a highly specific problem that I havent been able to find a viable answer for through the usual search engines. I am hoping someone here can help.
I am trying to autofilter a data set based on criteria that I parse from a Countifs formula. So the countif formula is as follows:
This would then come up with an answer of say 6 matching rows. The macro is then supposed to filter column A for "Monday", column B for "AM" and so on, and hopefully return the 6 rows that the countifs counted.
I have managed all this except for in a specific scenario:
The countif formula is as follows:
Where "Curmon" is a named range which holds the value "Jan-13" (numeric value for 01/01/2013 formatted as MMM-YY)
However in the middle of the code, I have to separate out the "<>" from the "Curmon" and evaluate each separately, and then combine them to be "<>Jan-13".
When I have done this, the filter treats the answer as "<> 13/01/2013" and as I dont have any results matching that criteria, returns all of my data.
I appologise for the awful look of my code, it is a work in progress, and I am sure pretty much every stage can be made more efficient but I wanted it working first. Here are the key parts:
Range_Criteria(i,2) holds the Criteria element of each pairing within the Countifs formula.
IsRangeName is a function which checks if a string is a named range
All of the variables storing the data are string format, so pre_text, Post_Text and the array Range_Criteria are all strings.
Then at the end of the macro I loop using the following to filter the report, using in the problem example the string "<>Jan-13" for Criteria1.
Interestingly, the code works fine, if in the countif I use the string "<>Jan-13" directly. It is only when I calculate the string via VBA that the problem occurs. "<>" & "Jan-13" also does not work for the same reason.
I hope that makes sense to someone, appologies for the long post. Any questions, or more info on the code required, please fire away.
I am suffering from a highly specific problem that I havent been able to find a viable answer for through the usual search engines. I am hoping someone here can help.
I am trying to autofilter a data set based on criteria that I parse from a Countifs formula. So the countif formula is as follows:
=Countifs(A:A,"Monday",B:B,"AM",.....)
This would then come up with an answer of say 6 matching rows. The macro is then supposed to filter column A for "Monday", column B for "AM" and so on, and hopefully return the 6 rows that the countifs counted.
I have managed all this except for in a specific scenario:
The countif formula is as follows:
=Countifs("A:A", "<>" & Curmon)
Where "Curmon" is a named range which holds the value "Jan-13" (numeric value for 01/01/2013 formatted as MMM-YY)
However in the middle of the code, I have to separate out the "<>" from the "Curmon" and evaluate each separately, and then combine them to be "<>Jan-13".
When I have done this, the filter treats the answer as "<> 13/01/2013" and as I dont have any results matching that criteria, returns all of my data.
I appologise for the awful look of my code, it is a work in progress, and I am sure pretty much every stage can be made more efficient but I wanted it working first. Here are the key parts:
Code:
For i = 1 To Range_Count
Text = Range_Criteria(i, 2)
If InStr(Text, "&") > 0 Then
Pre_Text = Trim(Left(Text, InStr(Text, "&") - 1))
Post_Text = Trim(Right(Text, Len(Text) - InStr(Text, "&")))
If IsRangeName(Post_Text) Then
Set Nm_Rng = ThisWorkbook.Names(Post_Text).RefersToRange
Post_Text = Format(Nm_Rng.Value, Nm_Rng.NumberFormat)
Else
On Error GoTo Criteria_Not_Range1
Set Nm_Rng = Range(Post_Text)
If IsDate(Nm_Rng.Value) Then
Post_Text = Format(Nm_Rng.Value, Nm_Rng.NumberFormat)
Else
Post_Text = Nm_Rng.Value
End If
End If
Continue_To_Next_Criteria1:
Range_Criteria(i, 2) = Pre_Text & Post_Text
End If
Next i
Range_Criteria(i,2) holds the Criteria element of each pairing within the Countifs formula.
IsRangeName is a function which checks if a string is a named range
All of the variables storing the data are string format, so pre_text, Post_Text and the array Range_Criteria are all strings.
Then at the end of the macro I loop using the following to filter the report, using in the problem example the string "<>Jan-13" for Criteria1.
Code:
ThisWorkbook.Names("Table_Start").RefersToRange.AutoFilter Field:=Field_Count, Criteria1:=Range_Criteria(i, 2), Operator:=xlFilterValues
Interestingly, the code works fine, if in the countif I use the string "<>Jan-13" directly. It is only when I calculate the string via VBA that the problem occurs. "<>" & "Jan-13" also does not work for the same reason.
I hope that makes sense to someone, appologies for the long post. Any questions, or more info on the code required, please fire away.