Results 1 to 4 of 4

Thread: New to Excel VBA: update cell when row changes

  1. #1

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    New to Excel VBA: update cell when row changes

    Hello All,

    I've done some extensive VBA work in Access, but I now need some VBA help in Excel.

    Here's what I'd like to do...
    When a workbook is opened, popup asking for a "Enter your First and Last Name", which will be stored as a String >> this I can handle.

    What i need help is...
    Whenever a row is updated with data, I would like to add the shortdate and ":" and then [Name String] to row "L". I'm trying to add a custom tracking log to see who is updating rows of data in my excel spreadsheet.

    Any ideas?
    Thanks!

    PS: I'm using Excel 2000 / 2003

    [EDIT]:
    After a little more thought... I'd like to track changes in this manner...
    I have columns A-K that I'd like to monitor. If ANY of these fields are updated, THEN write the ShortDate & ":" & [String'd NAME] into the corresponding row on Sheet3 titled "Modifications"

    For example:

    if 'Jane Smith' updates B1632 on 2/23/06, on the sheet titled "Modifications", i'd like to see: 02/23/06:Jane Smith on row 1623 (In Column A is fine)

    if 'John Doe' updates G and K 22 on 2/24/06, on the sheet titled "Modifications, i'd like to see: 02/24/06:John Doe on row 22 (In column A is okay)

    Does this make sense? I guess the question is multi-parted.
    Last edited by mateo107; Feb 23rd, 2006 at 09:41 PM.


    -Matthew-

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: New to Excel VBA: update cell when row changes

    You just need to put code in the worksheet's change event. The loops make sure you capture every change if multiple cells are changed at once. MAKE SURE you put in a 'code in progress' indicator if you are changing cell values in this event, or you will create an infinite loop.
    VB Code:
    1. Option Explicit
    2.  
    3. Private bChanging As Boolean
    4.  
    5. Private Sub Worksheet_Change(ByVal Target As Range)
    6.  
    7.     Dim lRow As Long, lCol As Long
    8.    
    9.     If Not bChanging Then
    10.         For lRow = 1 To Target.Rows.Count
    11.             For lCol = 1 To Target.Columns.Count
    12.                 Debug.Print "Cell " & Target.Cells(lRow, lCol).AddressLocal & " was modified."
    13.                 bChanging = True
    14.                 'Your code here.
    15.             Next lCol
    16.         Next lRow
    17.         bChanging = False
    18.     End If
    19.  
    20. End Sub
    Last edited by Comintern; Feb 23rd, 2006 at 10:15 PM. Reason: Capitalize

  3. #3

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    Re: New to Excel VBA: update cell when row changes

    Thanks for a quick response... just a question (sorry, I'm such a n00b right now)...

    in the section of "your code here"... do you think you could provide some insight in terms of what code I'd put there...

    If i'm not mistaken, thats where I'd put the code to actually put the UName and Date into the worksheet called "Modifications", but I have no base syntax to go off of for this...

    Anything more anyone could provide would be MUCH appreciated!


    -Matthew-

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: New to Excel VBA: update cell when row changes

    Quote Originally Posted by mateo107
    in the section of "your code here"... do you think you could provide some insight in terms of what code I'd put there...

    If i'm not mistaken, thats where I'd put the code to actually put the UName and Date into the worksheet called "Modifications", but I have no base syntax to go off of for this...
    Yep. All you need to do is pull the row number out of the cell address, build a new cell address to write to, and write your output. Try this out:
    VB Code:
    1. Option Explicit
    2.  
    3. Private bChanging As Boolean
    4. Private sName As String
    5.  
    6. Private Sub Worksheet_Change(ByVal Target As Range)
    7.  
    8.     Dim lRow As Long, lCol As Long, sAddress As String
    9.     Dim sWriteTo As String, sRow As String, sOut As String
    10.    
    11.     If Not bChanging Then
    12.         For lRow = 1 To Target.Rows.Count
    13.             For lCol = 1 To Target.Columns.Count
    14.                 'Take out the $ markers.
    15.                 sAddress = Replace(Target.Cells(lRow, lCol).AddressLocal, "$", vbNullString)
    16.                 'Turn on the 'in progress' indicator.
    17.                 bChanging = True
    18.                 'Get the real row number.
    19.                 sRow = GetAbsoluteRow(sAddress)
    20.                 'Build the cell address to write to.
    21.                 sWriteTo = "A" & sRow
    22.                 'Build the output string.
    23.                 sOut = Format$(Now, "Short Date") & ":" & sName & " on row " & sRow & "."
    24.                 'Write it to the worksheet.
    25.                 Target.Parent.Range(sWriteTo).Value = sOut
    26.             Next lCol
    27.         Next lRow
    28.         'Turn off the indicator.
    29.         bChanging = False
    30.     End If
    31.  
    32. End Sub
    33.  
    34. Private Function GetAbsoluteRow(sAddress As String) As String
    35.  
    36.     Dim iCount As Integer, sTest As String, sRow As String
    37.    
    38.     For iCount = 1 To Len(sAddress)             'Loop until you find a number
    39.         sTest = Mid$(sAddress, iCount, 1)       'Test each character.
    40.         If IsNumeric(sTest) Then Exit For       'If it's a number, we're done.
    41.     Next iCount
    42.    
    43.     GetAbsoluteRow = Right$(sAddress, Len(sAddress) - (iCount - 1)) 'Return the number part.
    44.  
    45. End Function
    Last edited by Comintern; Feb 24th, 2006 at 08:22 AM. Reason: Fix bad quote tag.

Posting Permissions

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



Click Here to Expand Forum to Full Width