Results 1 to 5 of 5

Thread: Need a shorter VBA code: Hide and unhide sheets based on presence of name in list

  1. #1
    Member
    Join Date
    May 2020
    Posts
    66
    Rep Power
    5

    Need a shorter VBA code: Hide and unhide sheets based on presence of name in list

    I have a workbook having 7 sheets, named
    MS, CWS, 1, 2, 3, 4, 5


    On sheet MS there is a table in range C4:C8

    I want to hide unhide sheets on the basis of table, except the sheet "CWS"

    (Please see the attachment)


    here is the code:



    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
         
      'hide unhide sheet 1
         If [C4] <> "" Then
            Sheet3.Visible = True
         Else
            Sheet3.Visible = False
         End If
         
       'hide unhide sheet 2
         If [C5] <> "" Then
            Sheet4.Visible = True
         Else
            Sheet4.Visible = False
         End If
         
       'hide unhide sheet 3
         If [C6] <> "" Then
            Sheet5.Visible = True
         Else
            Sheet5.Visible = False
         End If
         
       'hide unhide sheet 4
         If [C7] <> "" Then
            Sheet6.Visible = True
         Else
            Sheet6.Visible = False
         End If
         
       'hide unhide sheet 5
         If [C8] <> "" Then
            Sheet7.Visible = True
         Else
            Sheet7.Visible = False
         End If 
    
    End Sub
    This code can be simplified, but I don't know how.

    Would you please help me to do so.
    Attached Images Attached Images
    Last edited by DocAElstein; 06-11-2020 at 11:59 AM. Reason: Code tags - in editor use # icon for - before posting: [code] your coding here [/code]

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,385
    Rep Power
    10
    Hi

    I do think you can simplify this much, but I would do it a little bit different


    _1 ) I assume you do not intend selecting or changing more than one cell at a time. But if you do that by accident then sometimes that can give unexpected results. One way to avoid such problems is as follows:
    VBA tells us the range that was selected by returning that range as range object in the range object variable, Target. The .Value Property applied to a range object, will either return a single value, or an array of values if the range object contains more than one value. So we can check for that, and exit the procedure for a multi cell selection
    _2) It is a good idea to initially have a code part which restrict the macro from running most of the coding if you are not selecting in your range of interest, C5 – C8. We can use the VBA Intersect function for this. Once again we use the Target range along with the range of interest to us, Range("C5:C8"). If we include these two ranges in the ( arguments , , , , ) of Intersect, then Intersect will try to return a range object corresponding to where they overlap. If they do not overlap, then we did not select within , C4 – C8 . In such a case, VBA Intersect function does not error, - it returns no range, which VBA sees as Nothing
    So we can check for that

    _3)
    Having done that, the only simplifications that I can think of are minimal.
    Once again I assume you are only selecting a single cell at any time. So The macro I have modified to just look at the value of the cell that you selected.
    We can select worksheets by their item number, which is the integer number counting from the left. We can determine the item number related to the row number of your selection quite easily.
    For example ,
    C4 has row 4. You want to check worksheets item 3 for that, ( I think that is your sheet name 1, possibly? ) so that item number is the (row number -1)


    Alan

    P.S. You might want to consider if Private Sub Worksheet_Change(ByVal Target As Range) works better than Private Sub Worksheet_SelectionChange(ByVal Target As Range)


    Code:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Rem 1 Exit sub if more than one cell selectd
        If IsArray(Target.Value) Then Exit Sub
    Rem 2 Exit sub if we do not select inside our range of interest
        If Application.Intersect(Target, Me.Range("C4:C8")) Is Nothing Then Exit Sub
    Rem 3 Do it
    Dim WsItmNmbr As Long: Let WsItmNmbr = Target.Row - 1
    'hide unhide worksheet
         If Target.Value <> "" Then
            Worksheets.Item(WsItmNmbr).Visible = True
         Else
            Worksheets.Item(WsItmNmbr).Visible = False
         End If
    
    
    
    ''hide unhide sheet 1
    '     If [C4] <> "" Then
    '        Sheet3.Visible = True
    '     Else
    '        Sheet3.Visible = False
    '     End If
    '
    '   'hide unhide sheet 2
    '     If [C5] <> "" Then
    '        Sheet4.Visible = True
    '     Else
    '        Sheet4.Visible = False
    '     End If
    '
    '   'hide unhide sheet 3
    '     If [C6] <> "" Then
    '        Sheet5.Visible = True
    '     Else
    '        Sheet5.Visible = False
    '     End If
    '
    '   'hide unhide sheet 4
    '     If [C7] <> "" Then
    '        Sheet6.Visible = True
    '     Else
    '        Sheet6.Visible = False
    '     End If
    '
    '   'hide unhide sheet 5
    '     If [C8] <> "" Then
    '        Sheet7.Visible = True
    '     Else
    '        Sheet7.Visible = False
    '     End If
    '
    End Sub
    
    Attached Files Attached Files
    Last edited by DocAElstein; 06-12-2020 at 02:11 AM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  3. #3
    Member
    Join Date
    May 2020
    Posts
    66
    Rep Power
    5

    Actual Case

    Sorry that I was unable to convey my message properly.

    Please find the attachment below. I have described my case in the attached excel file.
    Attached Files Attached Files

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,385
    Rep Power
    10
    Hi,
    Here’s your explanation form your file…_
    _..... sheet "1" is made for cell C4 (ABHISHEK), sheet "2" is made for cell C5 (ANCHAL)…………sheet "65" is made for cell C68 (SHAHNAWAZ HUSAIN)
    my requirement is:
    If I delete the content of any cell, the corresponding sheet should be hide automatically.
    Means, if C4 is blank, sheet 1 hides if C5 is blank, sheet 2 hides….
    if I select C6:C9 and delete it, the corresponding sheet should be deleted automatically
    Also If I fill the cell again, the corresponding sheet should reappear.
    If I paste or delete multiple name in one click, still the corresponding sheets should hide or unhide accordingly….


    I am slightly confused that you are talking about hiding and deleting sheets. Hiding and deleting are two very different things

    For now I will assume that you are meaning just hiding / unhiding..

    If my next shot at a solution is not what you want , then explain carefully again what you want. ( But I will not be able to reply for a couple of days, as I am busy elsewhere )




    I will answer this question for now
    If I delete the content of any cell, the corresponding sheet should be hide automatically.
    Means, if C4 is blank, sheet 1 hides if C5 is blank, sheet 2 hides….
    if I select C6:C9 and delete the contents, the corresponding sheets should be hidden automatically
    Also If I fill the cell again, the corresponding sheet should reappear.
    If I paste or delete multiple names in one click, still the corresponding sheets should hide or unhide accordingly



    My previous macro will need to have the initial check for multiple cell selection removed.
    I think, a Private Sub Worksheet_Change(ByVal Target As Range) is preferable to a Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    The other change is not so substantial. The basic logic remains the same . I simply need to loop for each of the cells changed, and apply the same logic for each of those cells

    Code:
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    'Rem 1 Exit sub if more than one cell selectd
    '    If IsArray(Target.Value) Then Exit Sub
    Rem 2 Exit sub if we do not select inside our range of interest
        If Application.Intersect(Target, Me.Range("C4:C68")) Is Nothing Then Exit Sub
    Rem 3 Do it
    Dim WsItmNmbr As Long: ' Let WsItmNmbr = Target.Row - 1
    Dim RngCel As Range
        For Each RngCel In Target
         Let WsItmNmbr = RngCel.Row - 1
        'hide unhide worksheet
             If RngCel.Value <> "" Then
                Worksheets.Item(WsItmNmbr).Visible = True
             Else
                Worksheets.Item(WsItmNmbr).Visible = False
             End If
        Next RngCel
    End Sub

    Alan
    Attached Files Attached Files
    Last edited by DocAElstein; 06-18-2020 at 11:12 AM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  5. #5
    Member
    Join Date
    May 2020
    Posts
    66
    Rep Power
    5

    SOLVED!

    Works Perfectly!!

    The macro works perfectly in the way I need.

    Thanks a lot....and Sorry for the inconvenience caused.

Similar Threads

  1. Replies: 8
    Last Post: 06-01-2020, 06:13 PM
  2. macro to hide and unhide columns and rows
    By dkesar in forum Excel Help
    Replies: 1
    Last Post: 12-30-2014, 12:11 PM
  3. VBA To Hide And Unhide Rows
    By paul_pearson in forum Excel Help
    Replies: 10
    Last Post: 05-08-2013, 03:14 AM
  4. Hide/Unhide Columns with Listbox
    By obed_cruz in forum Excel Help
    Replies: 2
    Last Post: 05-28-2011, 07:26 PM
  5. Hide and Unhide Rows and Columns
    By Admin in forum Download Center
    Replies: 0
    Last Post: 05-11-2011, 12:00 AM

Posting Permissions

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