Results 1 to 5 of 5

Thread: [RESOLVED] Excel 97: onChange?

  1. #1

    Thread Starter
    Addicted Member Guru's Avatar
    Join Date
    May 2000
    Location
    sulking in the cupboard under the stairs
    Posts
    237

    Resolved [RESOLVED] Excel 97: onChange?

    I've got the following code in cell d1
    (Username() is a function in a module which gets the user's username)

    =IF(ISBLANK(A1),"",UserName())

    What I want is to set D1 to the user name only when A1 changes from blank to not-blank

    At the moment if A1 is non-blank D1 get updated whenever the spreadsheet is opened

    So I supose I need a sort of onchange event on A1
    How?

    Thanks
    Another light-hearted post from Guru

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel 97: onChange?

    You can use the _Change event of the worksheet - checking to see if A1 was the cell being changed. Then based on what change was made to A1 you can either add or remove the formula from cell D1.
    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.    
    3.     'Only continue if the user is
    4.     'changing cell A1
    5.     If Target.Address = "$A$1" Then
    6.         'If A1 is not blank then add
    7.         'the formula to D1
    8.         If Target.Value <> "" Then
    9.             Range("D1").Formula = "=IF(ISBLANK(A1),"""",UserName())"
    10.         'Otherwise remove the formula
    11.         Else
    12.             Range("D1").Formula =""
    13.         End If
    14.     End If
    15. End Sub
    Last edited by DKenny; Feb 13th, 2006 at 09:56 AM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Addicted Member Guru's Avatar
    Join Date
    May 2000
    Location
    sulking in the cupboard under the stairs
    Posts
    237

    Re: Excel 97: onChange?

    O...K...

    Thanks for the reply, it looks like we're heading in the right direction
    but I wasn't telling the whole truth.

    I need to cover the whole column so if A1 changes, D1 gets populated
    but also if ANY cell in column A changes the corresponding D cell needs to be set.

    Any ideas?

    Thanks
    Another light-hearted post from Guru

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel 97: onChange?

    OK
    In that case you need to check the Column number of the cell being changed to see if it is column A and then change the formula in the cell 3 to the right of that cell, i.e. in column D.
    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.    
    3.     'Only continue if the user is
    4.     'changing column A
    5.     If Target.Column = 1 Then
    6.         'If the cell in column A
    7.         'is not blank then add
    8.         'the formula to the coresponding
    9.         'cell in column D
    10.         If Target.Value <> "" Then
    11.             Target.Offset(0, 3).Formula = "=IF(ISBLANK(A1),"""",UserName())"
    12.         'Otherwise remove the formula
    13.         Else
    14.             Target.Offset(0, 3).Formula = ""
    15.         End If
    16.     End If
    17.    
    18. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Addicted Member Guru's Avatar
    Join Date
    May 2000
    Location
    sulking in the cupboard under the stairs
    Posts
    237

    Re: Excel 97: onChange?

    Thanks DKenny

    I was just working on it myself and came up with this:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Column = 1 Then
        
            If Target.Value <> "" Then
                Target.Worksheet.Cells(Target.Row, 4) = UserName()
               
            Else
                Target.Worksheet.Cells(Target.Row, 4) = ""
            End If
            
        End If
    End Sub

    Thanks for the help dude!
    Another light-hearted post from Guru

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