Consulting

Results 1 to 4 of 4

Thread: Solved: Automating Scipt

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Automating Scipt

    I found the following snippet and am trying to adopt it for my purposes. What I want to do is use the trigger cell (c2) to automatically clear the target range once the value of C2 is changed to "Yes" and then to automatically change the trigger cell back to "No."

    I thought from the name of the Sub that it would place some sort of button for me to press to execute the code without having to go through so many different steps. On after thought, if there is a way to automate the entire process, as described above, that would be preferable.

    [VBA]
    Sub CommandButton_Click()
    If [c2] = "Yes" Then
    [C4:C500].ClearContents
    End If
    End Sub
    [/VBA]

    Thanks,

    Opv

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "C2" '<== change to suit

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target

    If .Value = "Yes" Then

    Me.Range("C4:C500").ClearContents
    .Value = "No"
    End If
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub
    [/vba]

    This is worksheet event code, which means that it needs to be
    placed in the appropriate worksheet code module, not a standard
    code module. To do this, right-click on the sheet tab, select
    the View Code option from the menu, and paste the code in.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    Right click on the Tab for the sheet you'd like to run the routine.
    Paste the following:
    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C2")) Is Nothing Then
    If Target = "Yes" Then
    Range("C4:C500").ClearContents
    Target = "No"
    End If
    End If
    End Sub[/VBA]

  4. #4
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks to both of you. Both scripts work like a charm.

    Opv

Posting Permissions

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