Advanced filter error - vba

I am trying to create a unique list but the first value in the list is a blank and that seems to be causing me a problem. When I use a basic advanced filter it essentially just names the range 'Extract' and copies the formatting. So F2 equals `Extract' as below,
I have tried many approaches using the criteria range but nothing seems to works. Ideally I want this to be VBA code but the manual Advanced Filter is not working. To clarify I am searching like this,
I see a similar question here, it's just not working for me. Does anyone have any ideas what I am doing wrong? Is it the 'Copy into new location' option that is messing this up? Ideally I need it to be unique fields but I seem to be getting the same results, with or without the unique fields only box ticked.
Eventually I want this to be the code but, when I run the below in VBA I get a Run-Time error.
Range("E2:E5").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"h1:h2"), CopyToRange:=Range("F2"), Unique:=True
EDIT

Use <> for the criteria to exclude blanks.
btw, your List Range should be E1:E5 and H1 should be Offset values. Advanced Filters require a header.

Related

Multiple Criteria using autofilter on the same range, autofilter only takes last Criteria Used

I'm trying to filter multiple filters based on user input from a listbox on the same range.
However, for some reason the autosort method only sort the target range using the last criteria from testing.
I have searched everywhere, tested out array solutions (reading list info into an array), writing a range of values for filters on worksheets, changing variable type/operator to no avail. Nothing works.
Thanks for your time for reading this, would appreciate it if someone could help me with this.
dim lifecycle as range
dim List2String as string
Set lifeCycle = defineColRange(startWs, "Lifecycle Comments (Saks)", True, False)
For i = 0 To ListBox2_Lifecomments.ListCount - 1
'looping though the listbox2 to retrieve values
List2String = ListBox2_Lifecomments.List(i)
startWs.UsedRange.AutoFilter Field:=lifeCycle.Column, Criteria1:="<>" & List2String
Next i
startWs.UsedRange.SpecialCells(xlCellTypeVisible).Interior.Color = rgbLightPink 'testing to see if filter works
After some more digging, and rethinking, essentially I was asking the wrong questions.
The problem statement should be: "How to filter a range NOT equal to multiple criterias?"
It is documented here: Explaination of why .autosort doesn't work with not equal to with multiple criterias.
Few Solutions were discussed:
Use another a column to help you determine the result you want to achieve. In my example, it would be looping through all values from
user input, and output true/false on another column based on
comparison, then filter that helper column to work around this
problem.
Filter with an impossible values, this is situational. For example, if you want to filter numbers, set up an outofbound criteria for
non-numbers like given in the explanation link above.
Write code to hide the already filtered criteria, and keep filtering what is left with rest of criteria, 2 at a time.
Use advanced filters
Hope this helps others who might get into same problems in the future.

Is there a way to use VBA for Excel to output a formula to a specific cell in an Excel worksheet?

I am fairly new at programming. I feel like this should be a simple fix but I cannot find anything that works. I am trying to use a Select Case structure to write a different formula to a cell depending on the selected case. If I type:
Sheet1.Range("g10").Value = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
I get an error:
Run-time error '1004'
Application-defined or object-defined error.
I can get it to work if I include spaces like so:
Sheet1.Range("g10").Value = " =IF(SUM(F10)>0,SUM((F10-15)),"") "
but then Excel puts it into the cell like text, not as a formula (it doesn't do anything).
I have tried a few different methods but have run into the same issue. Is it possible to do this?
I apologize if this has been asked and answered, but I wasn't able to find anyone referencing this specific problem.
You don't need to sum a single cell ie SUM(F10) Also SUM((F10-15)) is the same as F10-15.
Try this: Sheet1.Range("g10").Formula = "=IF(F10>0,F10-15,"""")" Also note, you needed to double the double quotes towards the end.
Note, you can do this on a range too like this:
Sheet1.Range("g10:g20").Formula = "=IF(F10>0,F10-15,"""")" and it is smart enough to increment the references for you for the 10 cells.
Wrong property. You need .formula not .value
worksheets("sheet1").range("g10").formula = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
You should be able to type out the formula and then turn on the Macro Recorder and double-click on the cell with the forums, then turn off the Macro recorder and examine your code. That should give you what you want.

Range.Autofilter is NOT filtering correct column

I'm having a problem with my autofilter vba code where it's skipping the first column for some reason and taking field "4" as column E (the 5th column). This is extra odd because on some other files with the same exact setup, it doesn't do this, but for others it does. I can't put my finger on what the issue is (whether it's code or the actual spreadsheet). See the code below. Appreciate any help!
Workbooks(Num14).Sheets(1).Range("A:D").AutoFilter Field:=4, Criteria1:="REP"
I figured out the issue. I had to turn off autofiltermode first and then have the macro run the autofilter code. For some reason, the filter on the spreadsheet wasn't allowing for proper autofiltering. Hope this helps others!

Range("CustomTable").SpecialCells(xlCellTypeVisible).Delete now fails. Run-time error '1004'

was wondering if anyone noticed a change in behaviour of the following or similar code:
.Range("CustomTable").SpecialCells(xlCellTypeVisible).EntireRow.Delete
I use it to delete filtered range on a ListObjects table in excel 2013 and until about last week it was working fine; and now, if there are at least two non-sequential lines needed to be deleted, it is throwing an error: "Run-time error '1004': Delete method of Range class failed. I am sure it is not just the case that nothing is visible in filtered data-set, I ran it in debug and it definitely has multiple lines to delete, and it does give a normal entire row address with multiple lines to delete, but it fails to.
I have solved it by stripping out EntireRow and suppressing excel alerts on confirmation menu if I want to delete entire row. I am just quizzed why it suddenly stopped working?
I've come across this problem as well. What I've found to work is to save the range, remove the filter and then iterate through the areas of the range in reverse order (required as ranges change as you delete).
I've added in the "Application.Union" to take care of hidden columns. I just found a case of hidden columns creating multiple areas for the same row. So the solution to that is to get the SpecialCells range with the EntireRow, which still gives you duplicate areas for full rows. Then using the Application.Union you can compress these into a unique set of areas in a range.
Set delete_range = Application.Union(.Range("CustomTable").SpecialCells(xlCellTypeVisible).EntireRow, .Range("CustomTable").SpecialCells(xlCellTypeVisible).EntireRow)
.AutoFilter
Set delete_range =
For i = delete_range.Areas.Count To 1 Step -1
delete_range.Areas(i).EntireRow.Delete
Next
Hope that works for you. The disabling alerts didn't solve my problems. Everything else I tried had corner cases that didn't work. I assume you've already catered for removing the header (if you have one) from the range of interest.
NOTE: I also had another strange case which was resulting in the range = nothing. Can't remember the reasons for this happening, but I also included a check for nothing of the range before processing. I didn't include that in this answer.
Just to clarify: I have found a work around it, therefore not looking for one, I am just trying to understand why this line worked OK previously and doesn't work now.
In case anyone stumbles into this post whilst searching for the solution the line can be replaced by the following 3 lines, giving same result:
Application.DisplayAlerts = False
.Range("CustomTable").SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
P.S. I am personally not a big fan of suppressing alerts, but feel like this is the most optimal fix...

Using .Autofilter , using results in Textbox.value

Im using .autofilter to filter a list in excel, my problem is i want to have the results from the autofilter populate textboxes. i know i can use something similar to
textbox.text = Data(a1).value
however as the cells are filtered they keep there original cell location so i cant just use the cell location. There will only ever be one result to the filter so i need something that populates the text boxes from the row just under the filter heading.
for back ground the over all idea is to have a spread sheet that can be searched from a userform, which currently works, and then display the results in text boxes. Keeping the user of the spreadsheet away from the raw data as possible.
Maybe the function SpecialCells(xlCellTypeVisible) can help you... Your code could look like this:
Sheet1.Range.SpecialCells(xlCellTypeVisible).Cells(1,1).Value
I haven't tried it...
If you only will ever have 1 result, the following works:
textbox.text = Sheet1.Range("myrange").SpecialCells(xlCellTypeVisible).Value
but you will get a type mismatch if there is more than 1 result in the filter.