+ Reply to Thread
Results 1 to 7 of 7

Calling a Sub within a worksheet qualified with a variable of type Worksheet

  1. #1
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,797

    Calling a Sub within a worksheet qualified with a variable of type Worksheet

    I have a series of worksheets that each have a Sub declared that set the protection appropriately for that sheet:

    Please Login or Register  to view this content.
    I thought I would be able to look through all sheets like this:
    Please Login or Register  to view this content.
    However, this fails to compile because ProtectMe is not an attribute of the type Worksheet (apparently binding for Sub calls is required at compile time).

    I am trying to work around this. One approach is brute force, just call each individual worksheet. I could call Protect from a central place instead of using a ProtectMe Sub. Is there any way to do this and keep my (more elegant and flexible) approach? I was also thinking of using classes somehow, but haven't pursued that very far.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Calling a Sub within a worksheet qualified with a variable of type Worksheet

    This perhaps?
    Please Login or Register  to view this content.

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,797

    Re: Calling a Sub within a worksheet qualified with a variable of type Worksheet

    That code will execute but it will apply the same protection to all sheets. The reason I have a ProtectMe in each sheet is that the protection parameters can vary from sheet to sheet. I set it up this way to treat the ProtectMe sub as a method on each sheet, sort of treating Worksheet as a kind of a class. The objective is to allow each sheet to define its own protection.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Calling a Sub within a worksheet qualified with a variable of type Worksheet

    I don't really understand, your example was not tailored to individual sheets. Why not just loop through each sheet and apply protection as required?

  5. #5
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Calling a Sub within a worksheet qualified with a variable of type Worksheet

    Try:
    Please Login or Register  to view this content.
    Good luck.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,797

    Re: Calling a Sub within a worksheet qualified with a variable of type Worksheet

    Quote Originally Posted by StephenR View Post
    I don't really understand, your example was not tailored to individual sheets. Why not just loop through each sheet and apply protection as required?
    Each sheet has its own version of ProtectMe in its own module, and each version is called in a loop from another module. I am sorry I was not clear that the ProtectMe code is found in each sheet's module (though it's implementation can be different for each).

    In the meantime I have discovered that this works:
    Please Login or Register  to view this content.
    The reason that I prefer this solution is that I can add or remove sheets, tailor the code in each particular sheet, and not have to change the code shown here. That is, the loop doesn't care how many sheets there are, what their names are, or what their protection should be. It just asks each sheet to set its own protection as desired. Although it is not OOP, strictly speaking, I am treating each sheet as an instance of a class with a method ProtectMe.

    Thank you OnErrorGoto0 for your suggestion, I believe that will work. I will give it a try, though as I mentioned I have a workaround.

  7. #7
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Calling a Sub within a worksheet qualified with a variable of type Worksheet

    Yes - same principle
    Please Login or Register  to view this content.
    returns an Object, hence no issues with the compiler.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1