shift autofilter range in VBA

jerrykern

Board Regular
Joined
Jun 25, 2004
Messages
71
I'm trying to highlight an adjacent cell when a certain value is found in a cell given a range to look through. I used to do this with a simple loop, but now the data has gotten so large that looping takes forever. I'm trying an autofilter workaround to speed things up.

In short, what I think I need is to select a range using autofilter (always one column wide), and then set a new range, shifting one column to the right, so that I can highlight that column.

This is what I've got, but Excel doesn't like my "Set newrange...Offset"

Code:
Sub highlight()

Dim limitrows As Integer
Dim startrow As Integer
Dim whichcolumn As String
Dim highlightcolumn As String
Dim subject As String
Dim myrange As Range
Dim newrange As Range

whichcolumn = InputBox("Which column should I look in?", "Subject Column")
highlightcolumn = InputBox("Which column should I highlight when found?", "Highlight Column")
subject = InputBox("What should I look for?", "Subject")
startrow = Val(InputBox("Which row should I begin with?", "Start Row"))
limitrows = Val(InputBox("How many rows should I look through?", "Row Limit"))


Set myrange = Range(whichcolumn & startrow, whichcolumn & limitrows)


Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False


myrange.AutoFilter Range(whichcolumn & 1, whichcolumn & 1).Column, subject
Set newrange = myrange.Parent.AutoFilter.Range.Cells(1).Offset(0, 1)
newrange.Cells.Interior.Color = RGB(255, 255, 0)
myrange.Parent.AutoFilterMode = False


Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic


End Sub
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

jerrykern

Board Regular
Joined
Jun 25, 2004
Messages
71
....AAAAND I just answered my own question by removing the "Cells(1)" piece of the "Set newrange" line.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,163
Messages
5,527,163
Members
409,750
Latest member
BorisYeltsin

This Week's Hot Topics

Top