+ Reply to Thread
Results 1 to 4 of 4

Controling the Worksheet_Change Event?

  1. #1
    Forum Contributor
    Join Date
    03-24-2005
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    378

    Controling the Worksheet_Change Event?

    How can I control the worksheet change event? I only want it to actually run if a value in column 7 (G) is selected.

    I want the user to be able to enter data in column 7, run the worksheet_change event to update values in other columns in the same row.

    However, as I step through my macro, each time the macro updates the values in the other columns, the Worksheet_Change seems to run itself as a nested routine.

    If I update a dozen values based on the users entry, the change is nested a dozen times.

    I am sure there is a limit to how many times this can be down without crashing Excel.
    Thanks!
    Dennis

    I am using Windows 7 and Office 2007, all of my posts are based on this.

  2. #2
    Don Guillett
    Guest

    Re: Controling the Worksheet_Change Event?

    As always, post YOUR coding efforts for comments

    if target.column<> 7 then exit sub
    or
    if target.address<> "$A$7" then exit sub

    you may also need
    application.enableevents=false
    code
    application.enableevents=true

    --
    Don Guillett
    SalesAid Software
    [email protected]
    "DCSwearingen" <[email protected]>
    wrote in message
    news:[email protected]...
    >
    > How can I control the worksheet change event? I only want it to
    > actually run if a value in column 7 (G) is selected.
    >
    > I want the user to be able to enter data in column 7, run the
    > worksheet_change event to update values in other columns in the same
    > row.
    >
    > However, as I step through my macro, each time the macro updates the
    > values in the other columns, the Worksheet_Change seems to run itself
    > as a nested routine.
    >
    > If I update a dozen values based on the users entry, the change is
    > nested a dozen times.
    >
    > I am sure there is a limit to how many times this can be down without
    > crashing Excel.
    >
    >
    > --
    > DCSwearingen
    >
    > Getting old, but love computers.
    > ------------------------------------------------------------------------
    > DCSwearingen's Profile:
    > http://www.excelforum.com/member.php...o&userid=21506
    > View this thread: http://www.excelforum.com/showthread...hreadid=545561
    >




  3. #3
    Bob Phillips
    Guest

    Re: Controling the Worksheet_Change Event?

    '-----------------------------------------------------------------
    Private Sub Worksheet_Change(ByVal Target As Range)
    '-----------------------------------------------------------------
    Const WS_RANGE As String = "G:G"

    On Error GoTo ws_exit:
    Application.EnableEvents = False
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    'do your stuff
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub


    --
    HTH

    Bob Phillips

    (replace somewhere in email address with googlemail if mailing direct)

    "DCSwearingen" <[email protected]>
    wrote in message
    news:[email protected]...
    >
    > How can I control the worksheet change event? I only want it to
    > actually run if a value in column 7 (G) is selected.
    >
    > I want the user to be able to enter data in column 7, run the
    > worksheet_change event to update values in other columns in the same
    > row.
    >
    > However, as I step through my macro, each time the macro updates the
    > values in the other columns, the Worksheet_Change seems to run itself
    > as a nested routine.
    >
    > If I update a dozen values based on the users entry, the change is
    > nested a dozen times.
    >
    > I am sure there is a limit to how many times this can be down without
    > crashing Excel.
    >
    >
    > --
    > DCSwearingen
    >
    > Getting old, but love computers.
    > ------------------------------------------------------------------------
    > DCSwearingen's Profile:

    http://www.excelforum.com/member.php...o&userid=21506
    > View this thread: http://www.excelforum.com/showthread...hreadid=545561
    >




  4. #4
    Forum Contributor
    Join Date
    03-24-2005
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    378

    Thank You, for All of Your Help & Advice!

    As suggested by Don Guillett, here is the code I ended up with:

    Main module code:
    Option Explicit
    Declare Function GetUserName Lib "ADVAPI32.DLL" Alias "GetUserNameA" _
    (ByVal lpBuffer As String, nSize As Long) As Long
    Function LogonUserName()
    Dim S As String
    Dim n As Long
    Dim Res As Long
    Dim myName As String
    S = String$(200, 0)
    n = 199
    Res = GetUserName(S, n)
    myName = LCase(Left(S, n - 1))
    LogonUserName = UCase(Left(myName, 3))
    End Function


    Worksheet code
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "G:G"
    Dim nRow As Long
    On Error GoTo ws_exit:
    Application.EnableEvents = False
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    If IsEmpty(Target.Value) Then
    Cells(Target.Row, 9).Value = Null
    Cells(Target.Row, 8).Value = Null
    Else
    Cells(Target.Row, 9).Value = LogonUserName()
    Cells(Target.Row, 8).Value = Now
    nRow = Target.Row + 1
    InsertRow nRow
    End If
    Target.Select
    End With
    End If
    ws_exit:
    Application.EnableEvents = True
    End Sub
    '-----------------------------------------------------------------
    Private Sub InsertRow(nRow As Long)
    Worksheets("RMU").Unprotect password:="myPassword"
    If IsEmpty(Range(Cells(nRow, 6).Address).Value) Then
    If Not IsEmpty(Cells(nRow - 1, 7).Value) Then
    Rows(nRow).Insert Shift:=xlDown
    Rows(nRow - 2).Copy
    Rows(nRow).PasteSpecial Paste:=xlFormats, _
    Operation:=xlNone, SkipBlanks:= False, Transpose:=False
    Cells(nRow, 6).FormulaR1C1 = "=R[-1]C+1"
    Cells(nRow, 6).Copy
    Cells(nRow, 6).PasteSpecial Paste:=xlValues, _
    Operation:=xlNone, SkipBlanks:= False, Transpose:=False
    Application.CutCopyMode = False
    End If
    End If
    Worksheets("RMU").Protect password:="myPassword"
    End Sub

+ 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