+ Reply to Thread
Results 1 to 8 of 8

Toggle Macro on Button

Hybrid View

Baziwan Toggle Macro on Button 07-03-2010, 01:00 PM
Bob Phillips Re: Toggle Macro on Button 07-03-2010, 01:05 PM
Baziwan Re: Toggle Macro on Button 07-03-2010, 01:08 PM
foxguy Re: Toggle Macro on Button 07-04-2010, 05:36 PM
davesexcel Re: Toggle Macro on Button 07-04-2010, 05:52 PM
royUK Re: Toggle Macro on Button 07-05-2010, 01:39 AM
foxguy Re: Toggle Macro on Button 07-05-2010, 12:49 PM
royUK Re: Toggle Macro on Button 07-05-2010, 01:08 PM
  1. #1
    Registered User
    Join Date
    06-12-2010
    Location
    Hereford, England
    MS-Off Ver
    Excel 2003
    Posts
    73

    Toggle Macro on Button

    Hi Guys,

    How can I toggle this macro on a button click?

    Sub Button1_ToggleforEdit()
    
        ActiveSheet.Unprotect
        Range("B2").Select
        ActiveSheet.ListObjects("List1").Unlist
    End Sub
    Thanks in advance

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Toggle Macro on Button

    Toggle it to do what?

  3. #3
    Registered User
    Join Date
    06-12-2010
    Location
    Hereford, England
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: Toggle Macro on Button

    Quote Originally Posted by Bob Phillips View Post
    Toggle it to do what?
    When you click the button the first time it unprotects the sheet and converts the list to range.
    What I want is that when you click the button again, it adds the list and protects the sheet.

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Toggle Macro on Button

    Hi Baziwan;

    I don't know anything about ListObjects. I'm going to try and learn about them.
    But something like this should do what you want.
    Sub Button1_ToggleforEdit()
        Dim v
        Dim lErr as Long
    
        On Error Resume Next
        v = ActiveSheet.ListObjects("List1").Name
        lErr = Err.Number
        On Error Goto 0
    
        If lErr = 0 Then
            ActiveSheet.Unprotect
            Range("B2").Select
            ActiveSheet.ListObjects("List1").Unlist
        Else
            'I don't know how to create a ListObject with macros
            '   so change this line to do what you want
            With ActiveSheet.ListObjects.Add  
                 .Name = "List1"
            End With
            ActiveSheet.Protect
        End If
    End Sub
    I would really like to see the finished macro, so when you have fixed mine, please post it back here.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  5. #5
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Toggle Macro on Button

    Why not do it all at once?

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Toggle Macro on Button

    Add a button from the Controls ToolBox then use this code
    Private Sub CommandButton1_Click()
        With Me
            .Unprotect
            If .CommandButton1.Caption = "List Off" Then
                .ListObjects("List1").Unlist
                .CommandButton1.Caption = "List On"
            Else: .ListObjects.Add(xlSrcRange, Range("B6").CurrentRegion, , xlYes).Name _
                  = "List1"
                Application.CommandBars("List").Visible = False
                .CommandButton1.Caption = "List Off"
            End If
            .Protect
        End With
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Toggle Macro on Button

    Hi Roy;
    I really like your idea of changing the caption on the Button.
    What's the purpose of this line?
                Application.CommandBars("List").Visible = False
    I'm using XL2002, so I don't have ListObjects, but I'd like to know why you hide it.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Toggle Macro on Button

    That line switches off the List Toolbar, it's not essential.

+ Reply to Thread

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