shift autofilter range in VBA

jerrykern

Board Regular
Joined
Jun 25, 2004
Messages
75
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
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
....AAAAND I just answered my own question by removing the "Cells(1)" piece of the "Set newrange" line.
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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