Consulting

Results 1 to 19 of 19

Thread: VBA code to unhide Unhide multiple columns based on multiple values in row

  1. #1
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location

    VBA code to unhide Unhide multiple columns based on multiple values in row

    Hi Guys,

    I have data in "C:F" columns, which is hidden. when i copy "C4P" in cell B6, it should unhide only column C. If i copy "C4P,C2P,C4S,CS" in "B6:B137" cells it should unhide "C", "D", "E" and "F" columns.
    i got below code but it works for only one value in "B6:B137" and unhide one column.

    Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim Changed As Range
    
    
    Set Changed = Range("B6:B137")
    
    
    If Not Intersect(Target, Changed) Is Nothing Then
    Range("C:N").EntireColumn.Hidden = True
    Select Case UCase(Target.Value)
    Case "C2P"
    Range("C:C").EntireColumn.Hidden = False
    Case "CS"
    Range("D: D").EntireColumn.Hidden = False
    Case "C4P"
    Range("E:E").EntireColumn.Hidden = False
    Case "C4S"
    Range("F:F").EntireColumn.Hidden = False
    
    End Select
    
    
    End If
    
    
    Set Changed = Nothing
    
    
    End Sub
    Can someone help me to edit this code to achieve the above mentioned results.
    please find attachment for your reference.
    this is my first post, if any mistakes in this post, sorry for that.

    Thanks in advance.

    Murali K
    Attached Files Attached Files
    Last edited by Murali_K; 02-10-2019 at 11:49 PM. Reason: Added Code Tag

  2. #2
    VBAX Regular
    Joined
    Dec 2018
    Posts
    23
    Location
    Remove the End Select statement and paste the following code in:

    If Not Intersect(Target, Changed) Is Nothing Then 
      Range("C:N").EntireColumn.Hidden = True
    If UCase(Target.Value)  =  "C2P" Then Range("C:C").EntireColumn.Hidden = False
    If UCase(Target.Value)  = "CS" Then Range("D:D").EntireColumn.Hidden = False
    If UCase(Target.Value)  = "C4P" Then Range("E: E").EntireColumn.Hidden = False
    If UCase(Target.Value)  = "C4S" Then Range("F: F").EntireColumn.Hidden = False
    End If
    Last edited by MagPower; 02-11-2019 at 02:26 AM.

  3. #3
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    i used the above code, still its unhiding 1 column though there is 4 values in range "B6:B137". my expectation is to Unhide columns, equal to number of values in range "B6:B137".

  4. #4
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    i used the above code, still its unhiding 1 column though there is 4 values in range "B6:B137". my expectation is to Unhide columns, equal to number of values in range "B6:B137".
    Quote Originally Posted by MagPower View Post
    Remove the End Select statement and paste the following code in:

    If Not Intersect(Target, Changed) Is Nothing Then 
      Range("C:N").EntireColumn.Hidden = True
    If UCase(Target.Value)  =  "C2P" Then Range("C:C").EntireColumn.Hidden = False
    If UCase(Target.Value)  = "CS" Then Range("D:D").EntireColumn.Hidden = False
    If UCase(Target.Value)  = "C4P" Then Range("E: E").EntireColumn.Hidden = False
    If UCase(Target.Value)  = "C4S" Then Range("F: F").EntireColumn.Hidden = False
    End If

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    try:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B6:B137")) Is Nothing Then
      Columns("C:N").Hidden = True
      If InStr(1, Target.Value, "C2P", vbTextCompare) > 0 Then Columns("D").Hidden = False
      If InStr(1, Target.Value, "CS", vbTextCompare) > 0 Then Columns("E").Hidden = False
      If InStr(1, Target.Value, "C4P", vbTextCompare) > 0 Then Columns("F").Hidden = False
      If InStr(1, Target.Value, "C4S", vbTextCompare) > 0 Then Columns("G").Hidden = False
    End If
    End Sub
    or
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B6:B137")) Is Nothing Then
      Columns("D").Hidden = InStr(1, Target.Value, "C2P", vbTextCompare) = 0
      Columns("E").Hidden = InStr(1, Target.Value, "CS", vbTextCompare) = 0
      Columns("F").Hidden = InStr(1, Target.Value, "C4P", vbTextCompare) = 0
      Columns("G").Hidden = InStr(1, Target.Value, "C4S", vbTextCompare) = 0
    End If
    End Sub
    Last edited by p45cal; 02-11-2019 at 05:47 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    VBAX Regular
    Joined
    Dec 2018
    Posts
    23
    Location
        Dim Changed As Range
    
        With ActiveSheet
            Set Changed = .Range("B6:B137")
        End With
        
        If Not Intersect(Target, Changed) Is Nothing Then
          'Range("C:F").EntireColumn.Hidden = True
          If UCase(Target.Value) = "C2P" Then Range("C:C").EntireColumn.Hidden = False
          If UCase(Target.Value) = "CS" Then Range("D:D").EntireColumn.Hidden = False
          If UCase(Target.Value) = "C4P" Then Range("E:E").EntireColumn.Hidden = False
          If UCase(Target.Value) = "C4S" Then Range("F:F").EntireColumn.Hidden = False
          
          If UCase(Target.Value) = "ALL" Then Range("C:F").EntireColumn.Hidden = True
          If UCase(Target.Value) = "NONE" Then Range("C:F").EntireColumn.Hidden = False
        End If
    
        Set Changed = Nothing
    Ok, I made a few modifications to the code. (It now works for me.) Replace the old code with the above.

    =Russ

  7. #7
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    No Luck....Still its unhiding only one column
    Quote Originally Posted by p45cal View Post
    try:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B6:B137")) Is Nothing Then
      Columns("C:N").Hidden = True
      If InStr(1, Target.Value, "C2P", vbTextCompare) > 0 Then Columns("D").Hidden = False
      If InStr(1, Target.Value, "CS", vbTextCompare) > 0 Then Columns("E").Hidden = False
      If InStr(1, Target.Value, "C4P", vbTextCompare) > 0 Then Columns("F").Hidden = False
      If InStr(1, Target.Value, "C4S", vbTextCompare) > 0 Then Columns("G").Hidden = False
    End If
    End Sub
    or
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B6:B137")) Is Nothing Then
      Columns("D").Hidden = InStr(1, Target.Value, "C2P", vbTextCompare) = 0
      Columns("E").Hidden = InStr(1, Target.Value, "CS", vbTextCompare) = 0
      Columns("F").Hidden = InStr(1, Target.Value, "C4P", vbTextCompare) = 0
      Columns("G").Hidden = InStr(1, Target.Value, "C4S", vbTextCompare) = 0
    End If
    End Sub

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Supply a file with some sample data in column B.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    Ran modified code. still its unhiding only one column. i am just thinking of another approach. how to unhide multiple column, if column heading is entered in "B6:B137"?
    Quote Originally Posted by MagPower View Post
        Dim Changed As Range
    
        With ActiveSheet
            Set Changed = .Range("B6:B137")
        End With
        
        If Not Intersect(Target, Changed) Is Nothing Then
          'Range("C:F").EntireColumn.Hidden = True
          If UCase(Target.Value) = "C2P" Then Range("C:C").EntireColumn.Hidden = False
          If UCase(Target.Value) = "CS" Then Range("D:D").EntireColumn.Hidden = False
          If UCase(Target.Value) = "C4P" Then Range("E:E").EntireColumn.Hidden = False
          If UCase(Target.Value) = "C4S" Then Range("F:F").EntireColumn.Hidden = False
          
          If UCase(Target.Value) = "ALL" Then Range("C:F").EntireColumn.Hidden = True
          If UCase(Target.Value) = "NONE" Then Range("C:F").EntireColumn.Hidden = False
        End If
    
        Set Changed = Nothing
    Ok, I made a few modifications to the code. (It now works for me.) Replace the old code with the above.

    =Russ

  10. #10
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    SLC.xlsm
    Quote Originally Posted by p45cal View Post
    Supply a file with some sample data in column B.

  11. #11
    VBAX Regular
    Joined
    Dec 2018
    Posts
    23
    Location
    What version of Excel are you using?

  12. #12
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    Yes... its resulting the same as your code.

  13. #13
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    2016
    Quote Originally Posted by MagPower View Post
    What version of Excel are you using?

  14. #14
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    try:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Changed As Range
    Set Changed = Range("B6:B137")
    If Not Intersect(Target, Changed) Is Nothing Then
      Columns("C:N").Hidden = True
      For Each cll In Changed.Cells
        If InStr(1, cll.Value, "C2P", vbTextCompare) > 0 Then Columns("D").Hidden = False
        If InStr(1, cll.Value, "CS", vbTextCompare) > 0 Then Columns("G").Hidden = False
        If InStr(1, cll.Value, "C4P", vbTextCompare) > 0 Then Columns("E").Hidden = False
        If InStr(1, cll.Value, "C4S", vbTextCompare) > 0 Then Columns("F").Hidden = False
      Next cll
    End If
    End Sub
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    You realize that the last one in wins

    Capture.JPG

    If i copy "C4P,C2P,C4S,CS" in "B6:B137" cells it should unhide "C", "D", "E" and "F" columns.
    Did you mean D:G?


    Option Explicit
    'If i copy "C4P,C2P,C4S,CS" in "B6:B137" cells it should unhide "C", "D", "E" and "F" columns.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim s As String
        
        If Intersect(Target, Range("B6:B137")) Is Nothing Then Exit Sub
        
        s = UCase(Target.Cells(1, 1).Value)
        
        Application.EnableEvents = False
            
        Range("C:DN").EntireColumn.Hidden = True
            
        If InStr(s, "C2P") > 0 Then Range("D:D").EntireColumn.Hidden = False
        If InStr(s, "C4P") > 0 Then Range("E:E").EntireColumn.Hidden = False
        If InStr(s, "C4S") > 0 Then Range("F:F").EntireColumn.Hidden = False
        If InStr(s, "CS") > 0 Then Range("G:G").EntireColumn.Hidden = False
        Application.EnableEvents = True
    End Sub
    You can add the 'ALL' and 'NONE' option
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  16. #16
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    Wow.........This code is working. Thanks a lot.... You are awesome....
    Just one more question,
    i guess, if i have to add any additional columns, i can replace the column name in "If InStr(1, cll.Value, "C4S", vbTextCompare) > 0 Then Columns("F").Hidden = False" right?
    Quote Originally Posted by p45cal View Post
    try:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Changed As Range
    Set Changed = Range("B6:B137")
    If Not Intersect(Target, Changed) Is Nothing Then
      Columns("C:N").Hidden = True
      For Each cll In Changed.Cells
        If InStr(1, cll.Value, "C2P", vbTextCompare) > 0 Then Columns("D").Hidden = False
        If InStr(1, cll.Value, "CS", vbTextCompare) > 0 Then Columns("G").Hidden = False
        If InStr(1, cll.Value, "C4P", vbTextCompare) > 0 Then Columns("E").Hidden = False
        If InStr(1, cll.Value, "C4S", vbTextCompare) > 0 Then Columns("F").Hidden = False
      Next cll
    End If
    End Sub

  17. #17
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Quote Originally Posted by Murali_K View Post
    i guess, if i have to add any additional columns, i can replace the column name in "If InStr(1, cll.Value, "C4S", vbTextCompare) > 0 Then Columns("F").Hidden = False" right?
    Yes
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  18. #18
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    you are right. I could have mentioned it clearly in my question.Your code giving another possibility. Thanks for your help.
    Quote Originally Posted by Paul_Hossler View Post
    You realize that the last one in wins

    Capture.JPG



    Did you mean D:G?


    Option Explicit
    'If i copy "C4P,C2P,C4S,CS" in "B6:B137" cells it should unhide "C", "D", "E" and "F" columns.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim s As String
        
        If Intersect(Target, Range("B6:B137")) Is Nothing Then Exit Sub
        
        s = UCase(Target.Cells(1, 1).Value)
        
        Application.EnableEvents = False
            
        Range("C:DN").EntireColumn.Hidden = True
            
        If InStr(s, "C2P") > 0 Then Range("D:D").EntireColumn.Hidden = False
        If InStr(s, "C4P") > 0 Then Range("E:E").EntireColumn.Hidden = False
        If InStr(s, "C4S") > 0 Then Range("F:F").EntireColumn.Hidden = False
        If InStr(s, "CS") > 0 Then Range("G:G").EntireColumn.Hidden = False
        Application.EnableEvents = True
    End Sub
    You can add the 'ALL' and 'NONE' option

  19. #19
    VBAX Regular
    Joined
    Oct 2016
    Posts
    14
    Location
    Thank you very Much p45cal,MagPower and Paul_Hossler for your help.
    Have a great day Guys.................

Posting Permissions

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