0

I have a question regarding filtering text in columns in Excel / VBA. Here is my starting point (can't post images):

Column1 Column2 Column3
value1 bla bla
empty bla bla
value2 bla bla
value1 bla bla
empty ​ bla bla
value2 bla bla

What I want to do is to use a text filter on the "Column1" for "value1" so that the result includes the lines with the "empty" value in column1. The filtering result should look like that:

Column1 Column2 Column3
value1 bla bla
empty bla bla
value1 bla bla
empty ​ bla bla

However, when I use a normal text filter in Excel on column1 for "value1" I get this result, without the lines that have "empty" in Column1:

Column1 Column2 Column3
value1 bla bla
value1 bla bla

What I lack is a general idea of how this could be done in Excel / VBA. Somehow the lines with empty should be "connected" to those with "value1" in kind of of a range? Any suggestions very much appreciated!!

2
  • 2
    Filter has a "blanks" option: i.sstatic.net/cxQsE.png Commented Jul 14, 2021 at 16:12
  • @BigBen yes, thank you. However, this gives me all blanks, but I want only the blanks under the "good" values (in example above value1) and not those under the value2. So it should be kind of conditional... Commented Jul 14, 2021 at 21:32

2 Answers 2

1

Use the Macro Recorder and then some cleanup:

Sub Macro1()
    With Range("A1:C7")
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="=value1", _
            Operator:=xlOr, Criteria2:="="
    End With
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response - I think I am half way there. As far as I understand it will this bring me all the blanks in the table. However, I would like to get only those blanks that stand under a "good value" (value1) and NOT those under value2. So it should be kind of conditional... Is that possible?
0

You can set the same Filter with VBA that you can set using the Filter in Excel. Using the Macro-Recorder reveals the syntax.

Assuming that you want to filter data in Columns A to C, you can use

ActiveSheet.Range("$A:$C").AutoFilter Field:=1, Criteria1:="=value1", _ 
    Operator:=xlOr, Criteria2:="="

As you can see, the search for blank is done using the filter criteria "="

If you want to see all values for more than one value plus blanks, use

ActiveSheet.Range("$A:$C").AutoFilter Field:=1, _
      Criteria1:=Array("value1", "value2", "="), Operator:=xlFilterValues

you put all values that you want to see into an Array, and that Array is passed as parameter to the Criteria1. Again, blank is represented by "=".

Update If you want to use Autofilter to show all rows that either have a specific value or blank and the previous row has this value, you can solve this with a helper column. Assuming your criteria is in column A, put a formula in the helper column: =IF(A2="",A1,A2) and filter on this column.

If you don't want a helper column, you will need to write a VBA routine that checks row by row if to show or hide this row.

1 Comment

Thank you for the response - I think I am half way there. As far as I understand it, this will bring me all the blanks in the table. However, I would like to get only those blanks that stand under a "good value" (value1) and NOT those under value2. So it should be kind of conditional... Is that possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.