Brunoaricci
New Member
- Joined
- Feb 27, 2007
- Messages
- 7
I Thought of a way to improve on this idea:
****************
Original Post:
****************
Episode 839 - Filter by Selection
Shawn sends in a great tip for today's podcast. Access has a Filter by Selection icon. Using a few lines of VBA code, you can add this functionality to your personal macro workbook. Episode 839 shows you how.
Here is the code:
Sub Filter_by_Active_Cell()
Dim ColNum As Integer
ColNum = ActiveCell.Column - _
(ActiveCell.CurrentRegion.Column - 1)
Selection.AutoFilter Field:=ColNum, Criteria1:=ActiveCell
End Sub
Sub AutoFilterToggle()
Selection.AutoFilter
End Sub
This blog is the video podcast companion to the book, Learn Excel 97-2007 from MrExcel. Download a new two minute video every workday to learn one of the 377 tips from the book!
'**********************
'I added extra VBA after the 'show comment section
'this allows - the current filter to show in the comments section
' ****************************************
Sub Filter_by_Active_Cell()
Dim ColNum As Integer
ColNum = ActiveCell.Column - _
(ActiveCell.CurrentRegion.Column - 1)
Selection.AutoFilter Field:=ColNum, Criteria1:=ActiveCell
' show comment
Application.DisplayCommentIndicator = xlCommentAndIndicator
Dim w As Worksheet
Dim f As Integer
Dim filterset As String
Dim currentFiltRange As String
Dim myrange As Range
Set w = ActiveSheet
If w.AutoFilterMode = False Then
Application.DisplayCommentIndicator = xlNoIndicator
MsgBox ("Filters are not on. Turn on Data / Filter / AutoFilter and then run this macro again")
End
End If
Application.DisplayCommentIndicator = xlCommentAndIndicator
With w.AutoFilter
currentFiltRange = .Range.Address
With .Filters
' MsgBox ("You current Filtered DataRange to be processed is " & currentFiltRange & ". Press OK to Continue")
For f = 1 To .Count
filterset = ""
With .Item(f)
If .On Then
filterset = filterset & .Criteria1
If .Operator Then
filterset = filterset & .Operator
filterset = filterset & .Criteria2
End If
End If
End With
Set myrange = w.AutoFilter.Range(f)
On Error Resume Next
myrange.Comment.Delete
On Error GoTo 0
If filterset <> "" Then
myrange.AddComment filterset
With myrange.Comment
.Shape.TextFrame.Characters.Font.Size = 7
.Shape.TextFrame.AutoSize = True
.Shape.ScaleHeight 0.8, msoFalse, msoScaleFromTopRight
.Shape.Fill.ForeColor.SchemeColor = 5
.Shape.IncrementLeft -70
If .Shape.Height <= Rows(1).Height Then
.Shape.Top = Rows(1).Top + (Rows(1).Height - .Shape.Height) / 20
Else
.Shape.Top = Rows(1).Top - (.Shape.Height - Rows(1).Height) / 2
End If
End With
' MsgBox ("Current filter for column labeled as '" & myrange & "' " & filterset)
End If
Next f
End With
End With
End Sub
' **********************************************************
Sub AutoFilterToggle()
Selection.AutoFilter
' new - I added this to turn off comments
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
End Sub
I wish there was a way to have the Comment fit better in the CELL. but this is what I cam up with.
Any Ideas to improve this?
THanks,
Bruno
****************
Original Post:
****************
Episode 839 - Filter by Selection
Shawn sends in a great tip for today's podcast. Access has a Filter by Selection icon. Using a few lines of VBA code, you can add this functionality to your personal macro workbook. Episode 839 shows you how.
Here is the code:
Sub Filter_by_Active_Cell()
Dim ColNum As Integer
ColNum = ActiveCell.Column - _
(ActiveCell.CurrentRegion.Column - 1)
Selection.AutoFilter Field:=ColNum, Criteria1:=ActiveCell
End Sub
Sub AutoFilterToggle()
Selection.AutoFilter
End Sub
This blog is the video podcast companion to the book, Learn Excel 97-2007 from MrExcel. Download a new two minute video every workday to learn one of the 377 tips from the book!
'**********************
'I added extra VBA after the 'show comment section
'this allows - the current filter to show in the comments section
' ****************************************
Sub Filter_by_Active_Cell()
Dim ColNum As Integer
ColNum = ActiveCell.Column - _
(ActiveCell.CurrentRegion.Column - 1)
Selection.AutoFilter Field:=ColNum, Criteria1:=ActiveCell
' show comment
Application.DisplayCommentIndicator = xlCommentAndIndicator
Dim w As Worksheet
Dim f As Integer
Dim filterset As String
Dim currentFiltRange As String
Dim myrange As Range
Set w = ActiveSheet
If w.AutoFilterMode = False Then
Application.DisplayCommentIndicator = xlNoIndicator
MsgBox ("Filters are not on. Turn on Data / Filter / AutoFilter and then run this macro again")
End
End If
Application.DisplayCommentIndicator = xlCommentAndIndicator
With w.AutoFilter
currentFiltRange = .Range.Address
With .Filters
' MsgBox ("You current Filtered DataRange to be processed is " & currentFiltRange & ". Press OK to Continue")
For f = 1 To .Count
filterset = ""
With .Item(f)
If .On Then
filterset = filterset & .Criteria1
If .Operator Then
filterset = filterset & .Operator
filterset = filterset & .Criteria2
End If
End If
End With
Set myrange = w.AutoFilter.Range(f)
On Error Resume Next
myrange.Comment.Delete
On Error GoTo 0
If filterset <> "" Then
myrange.AddComment filterset
With myrange.Comment
.Shape.TextFrame.Characters.Font.Size = 7
.Shape.TextFrame.AutoSize = True
.Shape.ScaleHeight 0.8, msoFalse, msoScaleFromTopRight
.Shape.Fill.ForeColor.SchemeColor = 5
.Shape.IncrementLeft -70
If .Shape.Height <= Rows(1).Height Then
.Shape.Top = Rows(1).Top + (Rows(1).Height - .Shape.Height) / 20
Else
.Shape.Top = Rows(1).Top - (.Shape.Height - Rows(1).Height) / 2
End If
End With
' MsgBox ("Current filter for column labeled as '" & myrange & "' " & filterset)
End If
Next f
End With
End With
End Sub
' **********************************************************
Sub AutoFilterToggle()
Selection.AutoFilter
' new - I added this to turn off comments
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
End Sub
I wish there was a way to have the Comment fit better in the CELL. but this is what I cam up with.
Any Ideas to improve this?
THanks,
Bruno