Results 1 to 2 of 2

Automaticly hide columns if certain cell has value no

Threaded View

  1. #1
    Registered User
    Join Date
    09-05-2014
    Location
    Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    2

    Automaticly hide columns if certain cell has value no

    Cost Tracker.xlsmHi all,

    I am new to this forum because normally i can find the answers to all my excel questions without having to post the thread myself, however i am stuck here.

    I am making an excel sheet for work and i have made 2 worksheets, 1 containing the lists that i would like to have in my dropdown menus, and a worksheet in where i have implemented these drowdown menus.

    The sheet i am making contains a large amount of colums that a lot of times do not need to be filled out.
    I have created a drowdown menu in a cell in my worksheet, based on the information in the list worksheet via data validation, list, source=REVLIST.
    In order to allow my coworkers to add information to this dropdown list the following code is added to my worksheet:



    Private Sub Worksheet_Change(ByVal Target As Range)
    
          If Target.Address(False, False) = "$AN$2" Then
    Select Case Target.Value
        Case "No": Columns("AO:AQ").Hidden = True:
        Case "Yes": Columns("AO:AQ").Hidden = False:
       
    On Error Resume Next
    On Error Resume Next
    Dim ws As Worksheet
    Dim str As String
    Dim i As Integer
    Dim rngDV As Range
    Dim rng As Range
    Dim lCol As Long
    Dim myRsp As Long
    Dim strList As String
    If Target.Count > 1 Or Target.Value = "" Then Exit Sub
      
    If Target.Row > 1 Then
       If Target.Validation.Type <> 3 Then Exit Sub
      str = Target.Validation.Formula1
      str = Right(str, Len(str) - 1)
      
      Set rng = ThisWorkbook.Names(str).RefersToRange
      If rng Is Nothing Then Exit Sub
       Set ws = rng.Parent
      
      If Application.WorksheetFunction _
        .CountIf(rng, Target.Value) Then
        Exit Sub
      Else
       myRsp = MsgBox("Add this item to the drop down list?", _
          vbQuestion + vbYesNo + vbDefaultButton1, _
          "New Item -- not in drop down")
       If myRsp = vbYes Then
          lCol = rng.Column
          i = ws.Cells(Rows.Count, lCol).End(xlUp).Row + 1
          ws.Cells(i, lCol).Value = Target.Value
          
          strList = ws.Cells(1, lCol).ListObject.Name
       
          With ws.ListObjects(strList).Sort
             .SortFields.Clear
             .SortFields.Add _
                 Key:=Cells(2, lCol), _
                 SortOn:=xlSortOnValues, _
                 Order:=xlAscending
             .Header = xlYes
             .MatchCase = False
             .Orientation = xlTopToBottom
             .SortMethod = xlPinYin
             .Apply
          End With
          
          With ws.ListObjects(strList)
            .Resize .DataBodyRange.CurrentRegion
          End With




    End Sub
    End If
    End If







    And the following code has been entered in the list


    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim strList As String
    On Error Resume Next
    strList = Cells(1, Target.Column).ListObject.Name
    
    If strList <> "" Then
       Application.ScreenUpdating = False
        With ListObjects(strList).Sort
          .SortFields.Clear
          .SortFields.Add _
              Key:=Cells(2, Target.Column), _
              SortOn:=xlSortOnValues, _
              Order:=xlAscending
          .Header = xlYes
          .MatchCase = False
          .Orientation = xlTopToBottom
          .SortMethod = xlPinYin
          .Apply
        End With
        
       With ListObjects(strList)
         .Resize .DataBodyRange.CurrentRegion
       End With
    End If
    Application.ScreenUpdating = True
        
    End Sub
    
    Now in my worksheet i would like to add a code that hides colums AO:AQ if cell AN2 = no (the no being selected from the dropdown menu)
    Can anybody please help me with the code for this and how to implement it in the current change code that is already in place?

    Thanks in advance.

    Regards,

    Robin
    Last edited by RobinTielens; 09-09-2014 at 04:28 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. how to import a worksheet and automaticly have some columns moved
    By christop in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-27-2013, 08:03 AM
  2. Replies: 6
    Last Post: 11-14-2007, 11:55 AM
  3. [SOLVED] How to select automaticly the last cell not empty of a area?
    By Bigmas in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-12-2006, 09:55 AM
  4. [SOLVED] Can rows/columns Automaticly Grow/Shrink?
    By Brw in forum Excel General
    Replies: 0
    Last Post: 03-24-2006, 10:35 AM
  5. How to automaticly copy a cell every minute
    By nguyen in forum Excel General
    Replies: 5
    Last Post: 03-07-2005, 04:56 PM

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