Highlighting certain cells after appling filter

Motoracer88

New Member
Joined
Apr 25, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, first time posting. Just want to preface with saying that I have no training in VBA. I just started playing around with it to make formatting a spreadsheet at work go a little quicker. What I'm trying to do is, after applying a filter, I want to highlight a certain group of cells containing data. I ran the "Record Macro" feature and got the following code. The part I can't seem to figure out is the Range. It needs to be dynamic because the number of rows to highlight will change from week to week as will the actual row number. I've been scouring the internet and I've tried about a dozen different things but I either get an error message or it doesn't highlight the cells that I want. Any help would be much appreciated.
VBA Code:
Sub Macro3()
'
' Macro3 Macro
'

'
    ActiveSheet.Range("$A$2:$H$1000").AutoFilter Field:=2, Criteria1:="KY100"
    Range("A321:E714").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub
2024-04-24_21-33-19.jpg
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
try:
VBA Code:
Sub test()
    Application.ScreenUpdating = False
    Dim rng As Range
    Dim FilterCol As Range, cll As Range
    Dim i As Integer
    Dim myCriteria As String, rngcol() As String
    On Error GoTo iferr
    Set FilterCol = Application.InputBox("What column do you need to filter?", , , , , , , 8)
    Set rng = ActiveSheet.Range("A2:H" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row)
    rng.Offset(1).Resize(rng.Rows.Count - 1, 5).Interior.Pattern = xlNone
    If Not ActiveSheet.FilterMode Then rng.AutoFilter
    For Each cll In FilterCol
        rngcol = Split(cll.Address, "$")
        myCriteria = Application.InputBox("Enter your criteria for column " & rngcol(1) & ":", , , , , , , 2)
        rng.AutoFilter Field:=cll.Column, Criteria1:=myCriteria
    Next cll
    rng.Offset(1).Resize(rng.Rows.Count - 1, 5).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 0)
iferr:
    Application.ScreenUpdating = True
End Sub
1.Run this code
2.Select a cell that has column that you want to filter (can hold Ctrl to select more than a cell)
3.enter criteria for each column filter
 
Upvote 0
Thanks for the reply @eiloken. So I tried running this code when I got into work today. The only thing that happens is an Input box pops up, I enter my KY100 criteria, then nothing. I tried selecting the entire column B then running it, and just a group of cells that contains at least one occurrence of the KY100 filter criteria but nothing happens after I hit Ok on the Input box. I don’t get any error messages and nothing gets highlighted in yellow on the VBS editor window, so seems that it’s running through the entire code. Any thoughts?
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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