+ Reply to Thread
Results 1 to 8 of 8

Hide columns based on cell value - Unable to set hidden range property of Range Class

Hybrid View

  1. #1
    Registered User
    Join Date
    08-08-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    85

    Question Hide columns based on cell value - Unable to set hidden range property of Range Class

    test.xlsmHi

    I'm trying to write a very simple code (not using case select) to unhide columns based on variable entered into cell C5. (It's a number from 1 - 12 to represent a month)

    I tried using offset columns based on the month entered (so... if month is March (3)... ) but I'm getting a error message that I can't figure out....

    "Unable to set hidden range property of Range Class"... help?!

    Sub RunningAverageQualityScore()
    
    Dim iMonth As Integer 'review month
    Dim FinalColumn As Integer 'end of chart
    Dim FirstColumn As Integer 'beginning of chart
    Dim ColumnsHide As Integer 'counts columns to hide
    
    
    FinalColumn = Range("FinalColumn").Select
    FirstColumn = Range("FirstColumn").Select
    iMonth = Range("C5").Value
    ColumnsHide = -12 + iMonth
    
    Range("FinalColumn").Offset(0, ColumnsHide).Hidden = True
    
    
    
    End Sub
    Last edited by trillium; 05-12-2012 at 06:58 PM. Reason: add spreadsheet

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Hide columns based on cell value - Unable to set hidden range property of Range Class

    perhaps change your last line to

    Columns(Range("FinalColumn").Offset(0, ColumnsHide).Column).Hidden = True

  3. #3
    Registered User
    Join Date
    08-08-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Hide columns based on cell value - Unable to set hidden range property of Range Class

    hmmm... no longer get an error message but now nothing happens at all.... hmmmm....

  4. #4
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Hide columns based on cell value - Unable to set hidden range property of Range Class

    Easier if you post your workbook with description

  5. #5
    Registered User
    Join Date
    08-08-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Hide columns based on cell value - Unable to set hidden range property of Range Class

    Here it is! :-)
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Hide columns based on cell value - Unable to set hidden range property of Range Class

    Not exactly sure if this is what you are looking for since you didn't provide me with description of the issue, but try

    PS: Move your month value in D4 to C3.
    Cells.Columns.Hidden = False
    Columns(Range("C3") + 3).Hidden = True

  7. #7
    Registered User
    Join Date
    08-08-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Hide columns based on cell value - Unable to set hidden range property of Range Class

    hmmm just unhides all columns...???

  8. #8
    Registered User
    Join Date
    08-08-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Hide columns based on cell value - Unable to set hidden range property of Range Class

    GOT IT!!!

    Sub unhide()
    'hides columns based on value in cell C3 (month)
    
    Dim iMonth As Long
    Dim iStart As Long
    Dim iStop As Long
    
    iMonth = Range("C3").Value
    iStart = (4 + iMonth)
    iStop = 15
     
     'ensures all columns unhidden to start
        Columns("C:P").Select
        Selection.EntireColumn.Hidden = False
    
    'once we get to 12, all columns are now unhidden, so don't need code to hide
    If iMonth = 12 Then
        Columns("C:P").Select
        Selection.EntireColumn.Hidden = False
    Else
    
    'if <12, then hides unneeded columns
        Range(Columns(iStart), Columns(iStop)).Select
        Selection.EntireColumn.Hidden = True
        
    End If
    
    Range("C3").Select
    
    End Sub

+ 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