Shorten and tidy my code?

rob179994

New Member
Joined
Apr 9, 2014
Messages
8
How can i shorten this code? I think i can shorten the amount of If Else's to just one that does the same thing as what i have got but i dont know how to do this? Take a look.


Sub Filterofcolumn()
'
' Filterofcolumn Macro
'
Sheets("FolderSort").Select
ActiveSheet.Range("A1").Select

If Sheet1.ComboBox1.Value = "M6" Then
Sheets("FolderSort").Select
ActiveSheet.Range("$G$4:$G$368").AutoFilter Field:=1, Criteria1:=Sheet1.ComboBox1.Value
Sheets("FrontPage").Select

ElseIf Sheet1.ComboBox1.Value = "M55" Then
Sheets("FolderSort").Select
ActiveSheet.Range("$G$4:$G$368").AutoFilter Field:=1, Criteria1:=Sheet1.ComboBox1.Value
Sheets("FrontPage").Select

ElseIf Sheet1.ComboBox1.Value = "A66" Then
Sheets("FolderSort").Select
ActiveSheet.Range("$G$4:$G$368").AutoFilter Field:=1, Criteria1:=Sheet1.ComboBox1.Value
Sheets("FrontPage").Select

ElseIf Sheet1.ComboBox1.Value = "A595" Then
Sheets("FolderSort").Select
ActiveSheet.Range("$G$4:$G$368").AutoFilter Field:=1, Criteria1:=Sheet1.ComboBox1.Value
Sheets("FrontPage").Select

ElseIf Sheet1.ComboBox1.Value = "A595" Then
Sheets("FolderSort").Select
ActiveSheet.Range("$G$4:$G$368").AutoFilter Field:=1, Criteria1:=Sheet1.ComboBox1.Value
Sheets("FrontPage").Select

ElseIf Sheet1.ComboBox1.Value = "A590" Then
Sheets("FolderSort").Select
ActiveSheet.Range("$G$4:$G$368").AutoFilter Field:=1, Criteria1:=Sheet1.ComboBox1.Value
Sheets("FrontPage").Select

ElseIf Sheet1.ComboBox1.Value = "A585" Then
Sheets("FolderSort").Select
ActiveSheet.Range("$G$4:$G$368").AutoFilter Field:=1, Criteria1:=Sheet1.ComboBox1.Value
Sheets("FrontPage").Select

ElseIf Sheet1.ComboBox1.Value = "" Then
Sheets("FolderSort").AutoFilterMode = False

End If

End Sub


Thanks in advance!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Not sure, but if you flip round your if, so you remove the filter if blank,else use the filter:

I'm just on my hpone at moment so can't test, but something like:


Sub Filterofcolumn()

'
' Filterofcolumn Macro

If Sheet1.ComboBox1.Value = "" Then

Sheets("FolderSort").AutoFilterMode = False

else

Sheets("FolderSort").Select
ActiveSheet.Range("$G$4:$G$368").AutoFilter Field:=1, Criteria1:=Sheet1.ComboBox1.Value
Sheets("FrontPage").Select


End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,898
Messages
6,127,633
Members
449,392
Latest member
Kersh82

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