Filter VBA Left

Mark F

Well-known Member
Joined
Jun 7, 2002
Messages
513
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm looking to filter to show all rows where the left seven characters in cells in column Bare "Message"

Below is the code I have, but it is not working as I hope.

Thanks for any help

Mark
VBA Code:
Sub filter()

  
    ActiveSheet.ShowAllData
     lastRow = Cells(Rows.Count, "a").End(xlUp).Row
 
         ActiveSheet.Range("A:U").AutoFilter Field:=2, Criteria1:=Left(Cells(b2).Value, 7) = "Message", Operator:= _
        xlFilterValues
     

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try:

VBA Code:
Sub filter()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  ActiveSheet.Range("A:U").AutoFilter Field:=2, Criteria1:="Message*", Operator:=xlFilterValues
End Sub
 
Upvote 0
Mark,

I also recommend that you NOT use reserved words like "filter" as the name of your procedures, functions, or variables.
Doing so can cause errors and unexpected results, as when you refer to "filter" in your code, Excel may not be able to determine if you mean the function or your procedure.
I often preface my procedures with the word "My", i.e. "MyFilter" to ensure that I don't have this issue.
 
Upvote 2
Mark,

I also recommend that you NOT use reserved words like "filter" as the name of your procedures, functions, or variables.
Doing so can cause errors and unexpected results, as when you refer to "filter" in your code, Excel may not be able to determine if you mean the function or your procedure.
I often preface my procedures with the word "My", i.e. "MyFilter" to ensure that I don't have this issue.
Thanks for the tip - I wouldn't have considered that - I had changed the name to "filter" to post here (it was a longer name) but could quite easily have called it that in the live version.
 
Upvote 0
Mark, I'm filtering on the 21st column, but all the same. Here's what I have.

Sub cmbToggleFilter()

Sheets("Sheet1").Activate
Sheets("Sheet1").Select

Range("A1").Activate
ActiveCell.Select

If ActiveSheet.AutoFilterMode = True Then
Selection.AutoFilter
Else
With ActiveSheet.UsedRange
.AutoFilter Field:=21
.AutoFilter Field:=21, Criteria1:="=Message*", Operator:=xlAnd
End With
End If



ActiveSheet.Calculate
End Sub
 
Upvote 0
Try:

VBA Code:
Sub filter()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  ActiveSheet.Range("A:U").AutoFilter Field:=2, Criteria1:="Message*", Operator:=xlFilterValues
End Sub
Thanks - that works perfectly, and a simpler solution to the way I had approached it.
 
Upvote 0

Forum statistics

Threads
1,215,078
Messages
6,122,997
Members
449,093
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top