Been looking around a bit and haven’t found a proper solution as yet, so here’s my question! Still rather new to this, forgive me for any improper vocabulary.
I’m attempting to make a filter for data similar to the following:
8.5 x 11
8.3 x 10.9
8.7 x 10.9
8.3 x 10.9
11 x 8.5
8.5 x 11
8.5 x 11
15 x 11
Basically I need to be able to filter by the first value AND the second value. With a great deal of help from a member of Stackoverflow, I’ve sorted out placing these values into an array, determining the first and second value and assign them as Arrval(0) and Arrval(1) and then assign those oto the variables “small” and “large” based on the dimensions. That way if the first side is larger, etc. it still works out. Here is what think is the pertinent code in regards to that:
val = Trim(arr(r, 1))
If val Like "*x*" Then
Arrvals = Split(val, "x")
V1 = Trim(Arrvals(0))
V2 = Trim(Arrvals(1))
If IsNumeric(V1) And IsNumeric(V2) Then
V1 = CDbl(V1)
V2 = CDbl(V2)
If V1 > V2 Then
Small = V2: Large = V1
Else
Small = V1: Large = V2
End If
End If
End If
Subsequently I use these values to determine the dimensions of a “size” and count it if it meets certain parameters. e.g.
testcase8511 = Small <= 9.9 And Large <= 14.5
test117 = Small >= 10 And Large <= 17.6
I then count these using an if-else statement (just using if x then range.value = range.value +1) and change the color of the row they are present in.
Long story short (was hoping to give enough background to make this easier!) I need to be able to filter by these parameters and then export the results to a text file. I’ve got the text export mostly sorted out, but I can’t figure out how to filter properly. I suspect it’s mostly a syntax issue.
The quick and dirty way is to just have another cell in the row where these variables are true get an “x” or “y” or some other marker added to them when I’m doing the counting and then filter by that, but I suspect there is a better/more efficient/less resource intensive way to do this for someone who has more of a clue than I do. I don’t even know if literally using an autofilter, exporting and then turning the autofilter back off is the best way to do this. Since these rows are being color, I suppose I could also filter by the interior color of a cell or something else. Just not sure where to go to get this done properly.
So basically, in summary I need to be able to do
- filter by one of my above variables (e.g. testcase8511) after I’ve gone through all the data and counted them if true. (is there a way to store this info in VBA that makes sense resource wise? I doubt it, dunno.)
- Export this filtered data to text file (which I’ve mostly sorted for myself)
- Return the workbook (or leave it) visually the same. i.e. I don’t want this filter to be visually present to the user after the export process is completed.
- Do this as efficiently as possible, program wise (admittedly, I doubt anybody willing to answer would aspire to do otherwise). Some of the computers we’re using are… not great.
Thanks in advance! I’ll clarify anything I didn’t explain well if. Ask away.
Okay, here’s one solution for the filtering:
In the Worksheet-Module:
(
Fieldis the column number, must match withCell(i, "A")above)In a Module:
It seems to do the trick “OK”.
(I have used
WorksheetFunction.TextbecauseCStrgives a localized string like3,5in German.)From a practical perspective however, I would personally stick with the rule: If you want to work with numbers as numbers, they should be numbers! So I would rather do split the values into two columns and work with ‘normal’ numeric filters on these. (A sub for that would work pretty similar to the one here…)