Results 1 to 2 of 2

Worksheet_Change Macro To Hide Columns Based On A Range But Only When One Cell Is Edited

Threaded View

  1. #1
    Registered User
    Join Date
    02-18-2013
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    13

    Worksheet_Change Macro To Hide Columns Based On A Range But Only When One Cell Is Edited

    Hi Forum,

    Please find attached an example of what i'm trying to achieve (note this is a small portion of a much larger sheet).

    Essentially whenever a 'Y' or a 'N' is entered into a cell the macro would check a range of cells and only hide the columns if no other 'Y' 's are found in the range.

    This needs to then be replicated for a couple of ranges within the one worksheet. (see attached)

    I guessed the below which does not work:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Column = 1 Then
        Dim Options1 As Range
            For Each Options1 In Range("A5:A30")
                If Options1.Value = "y" Then
                Columns("B:C").EntireColumn.AutoFit
                ElseIf Options1.Value = "Y" Then
                Columns("B:C").EntireColumn.AutoFit
                Else
                Columns("B:C").ColumnWidth = 0
                End If
            Exit For
            Next Options1
            
            
    'ElseIf Target.Column = 5 Then
    '    Dim Options2 As Range
    '        For Each Options2 In Range("I46:I71")
    '            If Options2.Value = "y" Then
    '            Columns("J:K").EntireColumn.AutoFit
    '            ElseIf Options2.Value = "Y" Then
    '            Columns("J:K").EntireColumn.AutoFit
    '            Else
    '            Columns("J:K").ColumnWidth = 0
    '            End If
    '        Exit For
    '        Next Options2
            
            
    'ElseIf Target.Column = 9 Then
    '    Dim Options3 As Range
    '        For Each Options3 In Range("I46:I71")
    '            If Options3.Value = "y" Then
    '            Columns("J:K").EntireColumn.AutoFit
    '            ElseIf Options3.Value = "Y" Then
    '            Columns("J:K").EntireColumn.AutoFit
    '            Else
    '            Columns("J:K").ColumnWidth = 0
    '            End If
    '        Exit For
    '        Next Options3
    
    
    'ElseIf Target.Column = 13 Then
    '    Dim Options4 As Range
    '        For Each Options4 In Range("I46:I71")
    '            If Options4.Value = "y" Then
    '            Columns("J:K").EntireColumn.AutoFit
    '            ElseIf Options4.Value = "Y" Then
    '            Columns("J:K").EntireColumn.AutoFit
    '            Else
    '            Columns("J:K").ColumnWidth = 0
    '            End If
    '        Exit For
    '        Next Options4
    Else
    Exit Sub
    End If
    End Sub
    Many thanks in advance.

    -Ollie
    Attached Files Attached Files

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