Removing Select from code

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,941
Below is my code that changes my "cover" sheet based on a filter in the "detail" sheet. My "cover" sheet uses subtotals and has a combo box on there to select different regions. I would like to get rid of the .selects, but I can't figure out how to use With sheets("detail") to get rid of them.

Any help would be appreciated. Thanks.

Code:
Sub DropDown2_Change()
Application.ScreenUpdating = False
If (Range("C15").Value = 2) Then
Sheets("detail").Select
Selection.AutoFilter Field:=1, Criteria1:="North Central"
Sheets("cover").Select
ElseIf (Range("C15").Value = 1) Then
Sheets("detail").Select
Selection.AutoFilter Field:=1, Criteria1:="West"
Sheets("cover").Select
ElseIf (Range("C15").Value = 3) Then
Sheets("detail").Select
Selection.AutoFilter Field:=1, Criteria1:="South Central"
Sheets("cover").Select
ElseIf (Range("C15").Value = 4) Then
Sheets("detail").Select
Selection.AutoFilter Field:=1, Criteria1:="HDQ"
Sheets("cover").Select
ElseIf (Range("C15").Value = 5) Then
Sheets("detail").Select
Selection.AutoFilter Field:=1
Sheets("cover").Select
End If
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this:
Code:
Sub DropDown2_Change()
Application.ScreenUpdating = False
Select Case Range("C15").Value
    Case 2
        Sheets("detail").AutoFilter Field:=1, Criteria1:="North Central"
    Case 1
        Sheets("detail").AutoFilter Field:=1, Criteria1:="West"
    Case 3
        Sheets("detail").AutoFilter Field:=1, Criteria1:="South Central"
    Case 4
        Sheets("detail").AutoFilter Field:=1, Criteria1:="HDQ"
    Case 5
        Sheets("detail").AutoFilter Field:=1
End Select
Sheets("cover").Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0
You don't have to use WITH. With is only useful for doing SEVERAL things on one sheet...your doing like 1 thing on several sheets....kinda the opposite...
using Select Case might be helpful here too.
try

Code:
Sub DropDown2_Change() 
Application.ScreenUpdating = False 
Select Case Sheets("cover").Range("C15").Value
Case 2
    Sheets("detail").AutoFilter Field:=1, Criteria1:="North Central" 
Case 1
    Sheets("detail")..AutoFilter Field:=1, Criteria1:="West" 
Case 3 
    Sheets("detail").AutoFilter Field:=1, Criteria1:="South Central" 
Case 4
    Sheets("detail").AutoFilter Field:=1, Criteria1:="HDQ" 
Case 5
    Sheets("detail").AutoFilter Field:=1 
Case Else ' do nothing
End Select
Application.ScreenUpdating = True 
End Sub
 
Upvote 0
You don't have to use WITH. With is only useful for doing SEVERAL things on one sheet...your doing like 1 thing on several sheets....kinda the opposite...
using Select Case might be helpful here too.
try

Code:
Sub DropDown2_Change() 
Application.ScreenUpdating = False 
Select Case Sheets("cover").Range("C15").Value
Case 2
    Sheets("detail").AutoFilter Field:=1, Criteria1:="North Central" 
Case 1
    Sheets("detail")..AutoFilter Field:=1, Criteria1:="West" 
Case 3 
    Sheets("detail").AutoFilter Field:=1, Criteria1:="South Central" 
Case 4
    Sheets("detail").AutoFilter Field:=1, Criteria1:="HDQ" 
Case 5
    Sheets("detail").AutoFilter Field:=1 
Case Else ' do nothing
End Select
Application.ScreenUpdating = True 
End Sub

:LOL: I'm absolutely amazed how close we got to the same answer on this!! :LOL:

I was expecting someone to come up with a completely different solution! (y)
 
Upvote 0
Lewiy

Not quite completely different.:)
Code:
Sub DropDown2_Change()
Dim arrRegs

    Application.ScreenUpdating = False

    arrRegs = Array("West", "North Central", "South Central", "HDQ", "")
    Sheets("detail").AutoFilter Field:=1, Criteria1:=arrRegs(Range("C15").Value - 1)
    
    Sheets("cover").Select
    
    Application.ScreenUpdating = True

End Sub
Oh, and totally untested at the moment.:)

Kind of worried about that last filter but it'll take me some time to set up test data.
 
Upvote 0
For all 3 it does not like when there is Sheets("detail").Autofilter. Not sure why, but I had tried that also with no avail. It says Named Argument not found?
 
Upvote 0
I believe you have to specify a range for the AutoFilter.

Also Case Else 'do nothing
is not necessary because if it doesn't meet any of the conditions it won't do anything anway.
 
Upvote 0
Got it. Thanks all 3 solutions worked except Norie like you said it didn't like the last filter criteria.

Code:
Sheets("detail").Range("A4:H4").AutoFilter
 
Upvote 0
schielrn

There's nothing in your code to indicate where C15 was located, so I think some of us were just taking bit of a shot in the dark, and jonmo hit the target.:)

HOTPEPPER

I think you can use AutoFilter without a range but it needs to have been turned on.

And to turn it on you need to apply it to a range. :eek:

So you do need a range.:eek:

Oh no, I've just gone and confused/contradicted myself. :rolleyes:
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,080
Members
448,943
Latest member
sharmarick

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