+ Reply to Thread
Results 1 to 10 of 10

Unable to set the hidden property of the range class - Column Hiding

Hybrid View

chobson Unable to set the hidden... 05-19-2011, 08:50 AM
romperstomper Re: Unable to set the hidden... 05-19-2011, 09:14 AM
chobson Re: Unable to set the hidden... 05-19-2011, 09:44 AM
Domski Re: Unable to set the hidden... 05-19-2011, 09:15 AM
chobson Re: Unable to set the hidden... 05-19-2011, 09:45 AM
Domski Re: Unable to set the hidden... 05-19-2011, 09:52 AM
Whizbang Re: Unable to set the hidden... 05-19-2011, 09:48 AM
chobson Re: Unable to set the hidden... 05-19-2011, 09:53 AM
Whizbang Re: Unable to set the hidden... 05-19-2011, 10:18 AM
chobson Re: Unable to set the hidden... 05-19-2011, 10:40 AM
  1. #1
    Registered User
    Join Date
    05-19-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Unable to set the hidden property of the range class - Column Hiding

    I am working with the two pieces of code below. One hides rows when the sheet is activated and one hides columns when a combo box is updated. The error shows up when I activate the sheet, but when I debug, the error is referring to the combo box code and specifically the "Cells(1, indx).Columns.Hidden = DoHide" line.

    Ideas?

    Private Sub ComboBox10_Change()
    
        Dim indx As Integer, DoHide As Boolean
    
        Application.ScreenUpdating = False
        ActiveSheet.DisplayPageBreaks = False
        ActiveSheet.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False
       
        For indx = 2 To 200
            DoHide = (Cells(1, indx) = 0) And (Cells(1, indx) <> "")
            Cells(1, indx).Columns.Hidden = DoHide
        Next indx
        
        ActiveSheet.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False
        Application.ScreenUpdating = True
    
    
    End Sub
    
    Private Sub Worksheet_Activate()
    
        Dim indx As Integer, DoHide As Boolean
    
        Application.ScreenUpdating = False
        ActiveSheet.DisplayPageBreaks = False
        ActiveSheet.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False
       
        For indx = 2 To 200
            DoHide = (Cells(indx, 1) = 0) And (Cells(indx, 1) <> "")
            Cells(indx, 1).Rows.Hidden = DoHide
        Next indx
        
        ActiveSheet.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False
        Application.ScreenUpdating = True
    
    End Sub

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,980

    Re: Unable to set the hidden property of the range class - Column Hiding

    Cells(1, indx).EntireColumn.Hidden = DoHide
    should work.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    05-19-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Unable to set the hidden property of the range class - Column Hiding

    Quote Originally Posted by romperstomper View Post
    Cells(1, indx).EntireColumn.Hidden = DoHide
    should work.
    Thanks Robert...hmmmm. That didn't seem to work. Got the exact same error.

    Could it be that "indx" and "dohide" are both used by both pieces of code?

    Strange thing is that once I'm in the sheet and turn off the debugger, the code that hides the columns works fine (including now that I am using your EntireColumn.Hidden code). When I leave and come back, it would seem that the Worksheet.Activate code is trying to run but the error is showing up in the combo box code.

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Unable to set the hidden property of the range class - Column Hiding

    Do you not mean to unprotect your worksheet at the start of the code?

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    05-19-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Unable to set the hidden property of the range class - Column Hiding

    Quote Originally Posted by Domski View Post
    Do you not mean to unprotect your worksheet at the start of the code?

    Dom
    I think it *is* unprotecting the sheet at the start, no? We set everything to False at the end of the code while we're developing the model and then set Contents to True at the end of the code once we have finished the model.

    Do you think that unprotect code could be part of the run time issue I'm getting?

    Thanks!

  6. #6
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Unable to set the hidden property of the range class - Column Hiding

    You have this twice in both sets of code:

    ActiveSheet.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False

    It doesn't really seem to be doing anything. Not that it's necessarily causing your problem.

    Dom

  7. #7
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: Unable to set the hidden property of the range class - Column Hiding

    Change
        Application.ScreenUpdating = False
        ActiveSheet.DisplayPageBreaks = False
        ActiveSheet.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False
    To

        Application.ScreenUpdating = False
        ActiveSheet.DisplayPageBreaks = False
        ActiveSheet.UnProtect

  8. #8
    Registered User
    Join Date
    05-19-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Unable to set the hidden property of the range class - Column Hiding

    Quote Originally Posted by Whizbang View Post
    Change
        Application.ScreenUpdating = False
        ActiveSheet.DisplayPageBreaks = False
        ActiveSheet.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False
    To

        Application.ScreenUpdating = False
        ActiveSheet.DisplayPageBreaks = False
        ActiveSheet.UnProtect
    Thank you Whizbang.

    That is certainly more elegant protect code (I assume the corollary is ActiveSheet.Protect when I am ready to protect after the code runs?) but unfortunately I'm still getting the run time error when the page activates...

  9. #9
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: Unable to set the hidden property of the range class - Column Hiding

    Try putting in a breakpoint near the beginning of your Worksheet_Activate event code and then walk through it step by step. I copied the code from your original post exactly into a blank workbook with only a few zeros in row 1 and column 1. I then created a combobox.

    The code runs fine both times. So, the only thing I can think of is somehow your combobox10_Change event is triggering when a row is being hidden (???). I really can't make sense of that, but walk through your code and you should be able to see the point at which this occurs.

    Edit***

    It may be because your Cells(indx, 1) is returning an error of some sort. This then makes DoHide return an error. And then when you try to hide the row or column, you're saying .hidden = Error.

    Just a thought.
    Last edited by Whizbang; 05-19-2011 at 10:20 AM.

  10. #10
    Registered User
    Join Date
    05-19-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Unable to set the hidden property of the range class - Column Hiding

    Your intuition regarding the Cells(indx, 1) returning an error seems to have been the ticket!

    I changed the range in the WorkSheet.Activate code from "2 to 200" to "11 to 200" and it gave me the same error, but then when I changed it to "11 to 150" it worked fine!

    The strange thing is, I don't really know why. When this has happened in the past, I have gone through the cells and found that there was a letter or string in a cell when the code was expecting a number. This time, there were no offending characters that I could find anywhere...

    But, problem solved so we move on! Thanks everyone, with a special shout-out to Whizbang!

+ 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