PC Review


Reply
Thread Tools Rate Thread

Autofilter, protected worksheets, Debra Dalgleish's code

 
 
CB
Guest
Posts: n/a
 
      18th Sep 2008
Hi all,

I’m having issues with code that Debra Dalgleish provided on her website at
<http://www.contextures.com/xlautofilter03.html#Protect>. I don’t have a lot
of coding experience (none in Excel; some in Access; C programming years ago)
so I’m including my code below in case someone can spot errors.

Two questions:
1) The code works perfectly for the first two worksheets (which are
identical except for data). Drop-downs appear in the range A2:H2. I only need
it in B2 but I’m okay with that as that my data are in those columns. The
problem lies in the last two worksheets (which are different from the first 2
but identical to each other except for data). On those worksheets, only one
drop-down appears in B1, which is of no use to me! It makes no sense
whatsoever as I copied/pasted the code and simply changed worksheet names and
ranges.

2) Something is niggling in the back of my brain telling me that the code is
too long. Is there a more efficient way to accomplish the same thing as the
four WITH statements?

Thanks for any and all help!

Chris
……………………………………………………………..

Private Sub Workbook_Open()

'check for filter, turn on if none exists

With Worksheets("Depot_Inventory_Serialized")
If Not .AutoFilterMode Then
.Range("B2").AutoFilter
End If
.EnableAutoFilter = True
.Protect Password:="myPW", _
Contents:=True, UserInterfaceOnly:=True
End With

With Worksheets("Warehouse_Inventory_Serialized")
If Not .AutoFilterMode Then
.Range("B2").AutoFilter
End If
.EnableAutoFilter = True
.Protect Password:="myPW", _
Contents:=True, UserInterfaceOnly:=True
End With

With Worksheets("Depot_Inventory_non-Serial")
If Not .AutoFilterMode Then
.Range("B5").AutoFilter
End If
.EnableAutoFilter = True
.Protect Password:="myPW", _
Contents:=True, UserInterfaceOnly:=True
End With

With Worksheets("Warehouse_Inventory_non-Serial")
If Not .AutoFilterMode Then
.Range("B5").AutoFilter
End If
.EnableAutoFilter = True
.Protect Password:="myPW", _
Contents:=True, UserInterfaceOnly:=True

End With

End Sub

 
Reply With Quote
 
 
 
 
JP
Guest
Posts: n/a
 
      18th Sep 2008
In the last two code sections, the range is B5, not B2, does that make
a difference?

You could make the code shorter by using a For Each Loop, for example

Dim ws As Excel.Worksheet

For Each ws in Worksheets
If Not .AutoFilterMode Then
.Range("B2").AutoFilter
End If
.EnableAutoFilter = True
.Protect Password:="myPW", _
Contents:=True, UserInterfaceOnly:=True
Next ws


--JP

On Sep 18, 3:59*pm, CB <C...@discussions.microsoft.com> wrote:
> Hi all,
>
> I’m having issues with code that Debra Dalgleish provided on her website at
> <http://www.contextures.com/xlautofilter03.html#Protect>. I don’t have a lot
> of coding experience (none in Excel; some in Access; C programming years ago)
> so I’m including my code below in case someone can spot errors.
>
> Two questions:
> 1) The code works perfectly for the first two worksheets (which are
> identical except for data). Drop-downs appear in the range A2:H2. I only need
> it in B2 but I’m okay with that as that my data are in those columns. The
> problem lies in the last two worksheets (which are different from the first 2
> but identical to each other except for data). On those worksheets, only one
> drop-down appears in B1, which is of no use to me! *It makes no sense
> whatsoever as I copied/pasted the code and simply changed worksheet namesand
> ranges.
>
> 2) Something is niggling in the back of my brain telling me that the codeis
> too long. Is there a more efficient way to accomplish the same thing as the
> four WITH statements?
>
> Thanks for any and all help!
>
> Chris
> ……………………………………………………………..
>
> Private Sub Workbook_Open()
>
> 'check for filter, turn on if none exists
>
> With Worksheets("Depot_Inventory_Serialized")
> * If Not .AutoFilterMode Then
> * * .Range("B2").AutoFilter
> * End If
> * .EnableAutoFilter = True
> * .Protect Password:="myPW", _
> * Contents:=True, UserInterfaceOnly:=True
> End With
>
> With Worksheets("Warehouse_Inventory_Serialized")
> * If Not .AutoFilterMode Then
> * * .Range("B2").AutoFilter
> * End If
> * .EnableAutoFilter = True
> * .Protect Password:="myPW", _
> * Contents:=True, UserInterfaceOnly:=True
> End With
>
> With Worksheets("Depot_Inventory_non-Serial")
> * If Not .AutoFilterMode Then
> * * .Range("B5").AutoFilter
> * End If
> * .EnableAutoFilter = True
> * .Protect Password:="myPW", _
> * Contents:=True, UserInterfaceOnly:=True
> End With
>
> With Worksheets("Warehouse_Inventory_non-Serial")
> * If Not .AutoFilterMode Then
> * * .Range("B5").AutoFilter
> * End If
> * .EnableAutoFilter = True
> * .Protect Password:="myPW", _
> * Contents:=True, UserInterfaceOnly:=True
>
> End With
>
> End Sub


 
Reply With Quote
 
CB
Guest
Posts: n/a
 
      18th Sep 2008
Hi JP,

Thanks for your prompt reply! Yes, the cell reference does make a
difference. In the last two work sheets I have a few rows above my data so my
column headings start on row 5 as opposed to row 2 in the first two tables.

After some further fiddling around, I finally got it to work. I changed the
range references to A2:B2 and A5:B5. I then had to go to each worksheet,
unprotect it, turn OFF autofilter then close the file and save changes. The
next time I opened the file, I only had drop-downs in the two cells specified
by the range.

Seems weird to me but I don't use Excel a lot and it works so who am I too
complain.

Chris

"JP" wrote:

> In the last two code sections, the range is B5, not B2, does that make
> a difference?
>
> You could make the code shorter by using a For Each Loop, for example
>
> Dim ws As Excel.Worksheet
>
> For Each ws in Worksheets
> If Not .AutoFilterMode Then
> .Range("B2").AutoFilter
> End If
> .EnableAutoFilter = True
> .Protect Password:="myPW", _
> Contents:=True, UserInterfaceOnly:=True
> Next ws
>
>
> --JP
>
> On Sep 18, 3:59 pm, CB <C...@discussions.microsoft.com> wrote:
> > Hi all,
> >
> > I’m having issues with code that Debra Dalgleish provided on her website at
> > <http://www.contextures.com/xlautofilter03.html#Protect>. I don’t have a lot
> > of coding experience (none in Excel; some in Access; C programming years ago)
> > so I’m including my code below in case someone can spot errors.
> >
> > Two questions:
> > 1) The code works perfectly for the first two worksheets (which are
> > identical except for data). Drop-downs appear in the range A2:H2. I only need
> > it in B2 but I’m okay with that as that my data are in those columns. The
> > problem lies in the last two worksheets (which are different from the first 2
> > but identical to each other except for data). On those worksheets, only one
> > drop-down appears in B1, which is of no use to me! It makes no sense
> > whatsoever as I copied/pasted the code and simply changed worksheet names and
> > ranges.
> >
> > 2) Something is niggling in the back of my brain telling me that the code is
> > too long. Is there a more efficient way to accomplish the same thing as the
> > four WITH statements?
> >
> > Thanks for any and all help!
> >
> > Chris
> > ……………………………………………………………..
> >
> > Private Sub Workbook_Open()
> >
> > 'check for filter, turn on if none exists
> >
> > With Worksheets("Depot_Inventory_Serialized")
> > If Not .AutoFilterMode Then
> > .Range("B2").AutoFilter
> > End If
> > .EnableAutoFilter = True
> > .Protect Password:="myPW", _
> > Contents:=True, UserInterfaceOnly:=True
> > End With
> >
> > With Worksheets("Warehouse_Inventory_Serialized")
> > If Not .AutoFilterMode Then
> > .Range("B2").AutoFilter
> > End If
> > .EnableAutoFilter = True
> > .Protect Password:="myPW", _
> > Contents:=True, UserInterfaceOnly:=True
> > End With
> >
> > With Worksheets("Depot_Inventory_non-Serial")
> > If Not .AutoFilterMode Then
> > .Range("B5").AutoFilter
> > End If
> > .EnableAutoFilter = True
> > .Protect Password:="myPW", _
> > Contents:=True, UserInterfaceOnly:=True
> > End With
> >
> > With Worksheets("Warehouse_Inventory_non-Serial")
> > If Not .AutoFilterMode Then
> > .Range("B5").AutoFilter
> > End If
> > .EnableAutoFilter = True
> > .Protect Password:="myPW", _
> > Contents:=True, UserInterfaceOnly:=True
> >
> > End With
> >
> > End Sub

>
>

 
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
using Debra Dalgleish toolbar code chrisnsmith Microsoft Excel Misc 2 12th Feb 2009 05:30 PM
Help with Debra Dalgleish's Code prkhan56@gmail.com Microsoft Excel Programming 18 20th Feb 2007 12:28 AM
Debra Dalgleish Question VBA Noob Microsoft Excel Worksheet Functions 7 9th Nov 2006 10:40 PM
Debra Dalgleish =?Utf-8?B?bmM=?= Microsoft Excel Misc 14 12th May 2006 12:41 PM
Debra Dalgleish's DeleteMissingItems2002All() anthos.nicolaides@gmail.com Microsoft Excel Discussion 4 26th Jul 2005 07:32 AM


Features
 

Advertising
 

Newsgroups
 


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