+ Reply to Thread
Results 1 to 7 of 7

Prevent private sub macro from running

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-30-2008
    Location
    Eau Claire, WI
    Posts
    184

    Prevent private sub macro from running

    I need a line of code so that when I hit commandbutton2, the Private Sub Worksheet_Change(ByVal Target As Range) event macro on the same page DOESN'T run. The button clears certain lines, and when it runs the change event it ends up in an error, and I don't need it to run when hitting the commandbutton. Thanks!
    Last edited by jman0707; 11-15-2008 at 01:35 AM.

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    One option is to have the command button add text to a random cell when clicked (for example, add an "x" to cell Z1. Then you can add a test to the Worksheet_Change Event.

    A quick example:

    If Range("Z1") <> "x" then
      'Run the code
    End If

  3. #3
    Forum Contributor
    Join Date
    09-30-2008
    Location
    Eau Claire, WI
    Posts
    184
    Thanks! 2 quick questions. i've never had a button put text in a cell, what syntax does that use? And once I push the button, won't the text stay in the cell and then every time I change something in the worksheet it won't run the worksheet_change event? Or does it somehow test it, then I can get it to delete?

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Private Sub CommandButton1_Click()
            Application.ScreenUpdating = False
        If CommandButton1.Caption = "Macro ON" Then
            CommandButton1.Caption = "Macro OFF"
            CommandButton1.ForeColor = &HFFFF&
            CommandButton1.BackColor = &H808080
            Range("AA1").Value = ""
        Else: CommandButton1.Caption = "Macro ON"
            CommandButton1.ForeColor = &HFFFFFF
            CommandButton1.BackColor = &H800000
            Range("AA1").Value = 1
        End If
    End Sub
    Then BigBas' code adjusts to watch AA1 as so:
    If Range("AA1") = 1 then
      'Run the code
    End If
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Contributor
    Join Date
    09-30-2008
    Location
    Eau Claire, WI
    Posts
    184
    It can't be an off/on sort of thing, I need it the commandbutton to clear certain rows without triggering worksheet_change, then allow the worksheet_change event to be immediately active again. Here's what I put in the commandbutton code:

    Private Sub CommandButton2_Click()
    Application.ScreenUpdating = False
    Const pw As String = "password"
    Dim ce As Variant
    ActiveSheet.Unprotect pw
        Range("AA1").Value = "x"
        For Each ce In Sheets("CONTROL").Range("S129:S228")
        If ce.Value = 1 Then ActiveSheet.Cells(ce.Row - 112, "B").Resize(1, 13).ClearContents
        Next ce
     Application.ScreenUpdating = True
     ActiveSheet.Protect pw, AllowSorting:=True, AllowFiltering:=True
    End Sub
    And this in the workshee_change event right in the beginning:

    If Range("AA1").Value = "x" Then
        Application.ScreenUpdating = False
        Range("AA1").Value = ""
        Application.ScreenUpdating = True
        Exit Sub
    End If
    But it's still not quite working. Any ideas?
    Last edited by jman0707; 11-15-2008 at 01:16 AM.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    You haven't told us what you're deleting, so you'll have to edit the "clear" event in this code to delete what you want, but this commandbutton macro will work:
    Private Sub CommandButton1_Click()
            Application.EnableEvents = False
            Application.ScreenUpdating = False
            Range("C1:E10").Clear
            Application.EnableEvents = True
    End Sub

  7. #7
    Forum Contributor
    Join Date
    09-30-2008
    Location
    Eau Claire, WI
    Posts
    184
    Oh, Ok perfect! I think all I needed was the enableevents to be false so nothing else would run. Thanks!

+ 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