PC Review


Reply
Thread Tools Rate Thread

Autofilter using a value from a list.

 
 
N1KO
Guest
Posts: n/a
 
      29th May 2009
Hi,

I need to autofilter 2 lists on 2 sheets (Main & Revenue Costs).

Main's sheet is cells B4 - AH4 for the headings, list is "n" rows long

Revenue Costs sheet is cells A4 - M4 for the headings, again "n" rows long.

I need them to filter when a button a sheet (named "Front") is selects.

The value will be in Cell I6 of the "Front" sheet.

Is this possible?

Thanks in advance for any help with the VBA code.

 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      30th May 2009
Hi NIKO,

Use an ActiveX button on Sheet Front. If using xl2007 then on Developer
ribbon select Insert in the Controls block and select from the ActiveX
controls. If earlier version of xl then select button from the Controls
Toolbox Toobar (Not from the Forms toolbar). Design mode will switch on (Set
square, ruler and pencil in the button icon).

Right click the button while in Design mode and select View code. A default
sub name will appear for the click event on the button. This is the sub name
that must be used for the first sub of the following code so if the sub name
I have used does not match then use your default one.

Copy the following code into the VBA editor. Note there are 2 subs. The
first one belongs in the button click sub; the second one leave out on its
own below the first sub.

You will need to edit the filtNumb to the filter you want set. Count the
filters from the left for the number of the filter to set.

Close the VBA editor. (X with red background top right of VBA editor screen.)

On the worksheet, Click the Design mode button to tun it off. (Should change
from orange to blue.)

To get back to the code, right click the worksheet name tab and select View
code.

Private Sub CommandButton1_Click()

Dim crit1 As Variant 'Holds criteria cell values
Dim filtNumb As Integer 'Holds filter number to set

crit1 = Sheets("Front").Range("I6")

'Edit filter number to suit
'count filters from left for number.
filtNumb = 1

Call SetFilters("Main", crit1, filtNumb)

Call SetFilters("Revenue Costs", crit1, filtNumb)

End Sub


Sub SetFilters(strShtName, crit, filt)

With Sheets(strShtName)
'Ensure that AutoFilter is turned on.
'This avoids error problems if not turned on.
If .AutoFilterMode Then
If .FilterMode Then
.ShowAllData 'Remove existing filtering
End If
With .AutoFilter.Range
.AutoFilter Field:=filt, Criteria1:="=" & crit
End With
Else
MsgBox "AutoFilter not turned on for sheet " _
& strShtName & "." & vbCrLf & _
"Processing for sheet " & strShtName & " terminated."
End If
End With

End Sub

--
Regards,

OssieMac


 
Reply With Quote
 
N1KO
Guest
Posts: n/a
 
      1st Jun 2009
Thanks Ossie, I've had to tweak it abit as the filter on the 2nd sheet is in
a different place to the first but apart from that it works perfectly.

Appreciated.

"OssieMac" wrote:

> Hi NIKO,
>
> Use an ActiveX button on Sheet Front. If using xl2007 then on Developer
> ribbon select Insert in the Controls block and select from the ActiveX
> controls. If earlier version of xl then select button from the Controls
> Toolbox Toobar (Not from the Forms toolbar). Design mode will switch on (Set
> square, ruler and pencil in the button icon).
>
> Right click the button while in Design mode and select View code. A default
> sub name will appear for the click event on the button. This is the sub name
> that must be used for the first sub of the following code so if the sub name
> I have used does not match then use your default one.
>
> Copy the following code into the VBA editor. Note there are 2 subs. The
> first one belongs in the button click sub; the second one leave out on its
> own below the first sub.
>
> You will need to edit the filtNumb to the filter you want set. Count the
> filters from the left for the number of the filter to set.
>
> Close the VBA editor. (X with red background top right of VBA editor screen.)
>
> On the worksheet, Click the Design mode button to tun it off. (Should change
> from orange to blue.)
>
> To get back to the code, right click the worksheet name tab and select View
> code.
>
> Private Sub CommandButton1_Click()
>
> Dim crit1 As Variant 'Holds criteria cell values
> Dim filtNumb As Integer 'Holds filter number to set
>
> crit1 = Sheets("Front").Range("I6")
>
> 'Edit filter number to suit
> 'count filters from left for number.
> filtNumb = 1
>
> Call SetFilters("Main", crit1, filtNumb)
>
> Call SetFilters("Revenue Costs", crit1, filtNumb)
>
> End Sub
>
>
> Sub SetFilters(strShtName, crit, filt)
>
> With Sheets(strShtName)
> 'Ensure that AutoFilter is turned on.
> 'This avoids error problems if not turned on.
> If .AutoFilterMode Then
> If .FilterMode Then
> .ShowAllData 'Remove existing filtering
> End If
> With .AutoFilter.Range
> .AutoFilter Field:=filt, Criteria1:="=" & crit
> End With
> Else
> MsgBox "AutoFilter not turned on for sheet " _
> & strShtName & "." & vbCrLf & _
> "Processing for sheet " & strShtName & " terminated."
> End If
> End With
>
> End Sub
>
> --
> Regards,
>
> OssieMac
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Excel List specific data from a list? NOT Autofilter?? wx4usa Microsoft Excel Misc 8 21st Oct 2008 06:34 PM
List for AutoFilter and list for Subtotals have different meanings? Epinn Microsoft Excel New Users 2 18th Aug 2006 09:02 PM
Autofilter in List =?Utf-8?B?UiBLIEJvbGVz?= Microsoft Excel Misc 3 21st Jan 2006 02:23 PM
Get the list of data inside AutoFilter list ashleyyeung Microsoft Excel Programming 1 23rd Aug 2004 01:54 PM
Filter macro using criteria in a list (not autofilter drop down list) Jeff Smith Microsoft Excel Worksheet Functions 0 29th Jul 2004 10:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:47 AM.