Results 1 to 7 of 7

Thread: Combine two Subs into One (Using One Button)

  1. #1
    New Member
    Join Date
    Feb 2018
    Posts
    31
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Combine two Subs into One (Using One Button)

    Hi All

    I currently have a worksheet with two command buttons by pressing one it hides all columns in a range that are blank, the other unhides all columns in that range. Is there a way to combine the two so i can use just one button "Hide/Unhide"?

    This is the code i have to hide columns:

    Code:
    Dim ws As Worksheet Dim rng As Range
        
        Application.ScreenUpdating = False
        Sheet12.Unprotect ("passwordhere")
        Set ws = Sheet12
       
        For Each rng In ws.Range("D7:BB7")
            If rng.Value = "" Then
                rng.EntireColumn.Hidden = True
            End If
        Next rng
        Sheet12.Shapes("CommandButton1").Visible = False
        Sheet12.Shapes("CommandButton2").Visible = True
        Sheet12.Protect ("passwordhere")
    This is the code i have to unhide columns:

    Code:
    Sheet12.Unprotect ("passwordhere")
    Sheet12.Columns("D:BB").Hidden = False
    Sheet12.Shapes("CommandButton1").Visible = True
    Sheet12.Shapes("CommandButton2").Visible = False
    Sheet12.Protect ("passwordhere")
    Thanks in Advance
    Tom

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Combine two Subs into One (Using One Button)

    Are these ActiveX or Forms Control buttons?
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    New Member
    Join Date
    Feb 2018
    Posts
    31
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Combine two Subs into One (Using One Button)

    ActiveX

    Thanks
    Tom

  4. #4
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Combine two Subs into One (Using One Button)

    Forgot to ask, is the button on the sheet you are hiding the columns?
    Last edited by Fluff; Jul 21st, 2019 at 10:01 AM.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  5. #5
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Combine two Subs into One (Using One Button)

    Never mind, try
    Code:
    Sub CommandButton1_Click()
       Dim Cl As Range
       Me.Unprotect "Password"
       With Me.CommandButton1
          If .Caption = "Hide" Then
             For Each Cl In Me.Range("D7:BB7")
                If Cl.Value = "" Then Cl.EntireColumn.Hidden = True
             Next Cl
             .Caption = "Unhide"
          Else
             Me.Range("D:BB").EntireColumn.Hidden = False
             .Caption = "Hide"
          End If
       End With
       Me.Protect "Password"
    End Sub
    If the button is on a different sheet change the red Me to Sheet12
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  6. #6
    New Member
    Join Date
    Feb 2018
    Posts
    31
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Combine two Subs into One (Using One Button)

    Thank you!! Works perfectly Just what I wanted!

  7. #7
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: Combine two Subs into One (Using One Button)

    You're welcome & thanks for the feedback
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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