Consulting

Results 1 to 4 of 4

Thread: Excel 2003, worksheet_change to insert current date when a cell is colored yellow

  1. #1

    Excel 2003, worksheet_change to insert current date when a cell is colored yellow

    I've been given another small task, which I thought I could get working by using Worksheet_Change(ByVal Target As Range), but I can't get the sub to trigger.

    Cell A1 is already populated with a customer reference.
    When this file (physical file on desk, not computer file )has been completed, we highlight the reference in yellow.
    We also then have to put the date into column D (same row) (day of completion as such).
    I can't get the sub to trigger when the cells colour is changed.

    Is there another way of doing this??

    NOTE: This needs to work with Excel 2003. I'm not sure if this can be achieved by using conditional formatting??

    Thanks

    This is a little snippet of code that I have already

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Selection.Cells.Count > 1 Then
        Exit Sub
    End If
    
    
    If Target.Column = 1 Then
        Application.EnableEvents = False
    
    
            If Target.Interior.ColorIndex > 0 Then
    
    
            Target.Offset(0, 4).Value = Date
        End If
        Application.EnableEvents = True
    End If
    
    
    End Sub

    EDIT:

    I've actually just come up with this.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    'Watch column equal to 1 (Column A), if any cells are double clicked then continue with procedure
    If Target.Column = 1 Then
        Application.EnableEvents = False
    
    
    'If anything is currently inside the double clicked cell then continue with procedure
            If Target.Value > 0 Then
            
    'Change the colour of the cell to yellow
            Target.Interior.Color = 65535
            
    'Enter "Current Date" into Column B
            Target.Offset(0, 1).Value = Date
            
    'Correctly format the date
            Target.Offset(0, 1).NumberFormat = "dd.mm.yyyy"
            
        End If
        Application.EnableEvents = True
    End If
    End Sub
    This highlights the cell, and inserts the date when I double click on the reference... Which is good, but I would have to do this for each reference (sometimes I have 10 - 20 files that I have to mark as completed). I still think the highlighting method would be better, but can't figure out how to do it.
    Last edited by ashleyuk1984; 03-27-2014 at 03:27 AM.

  2. #2
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    I don't think you can fire an event just from formatting, but you can make a small Form that will color your cells and fire something at the same time. If you make a small UserForm you can call it from some shortcut keys and show it Modal, then you can select how ever many files you want to mark yellow and in this example it will put the date in the next column over. This only fires if you have a cell or cells selected in column A, otherwise you get a warning.

    Sub ShowModal()
    UserForm1.Show 0
    End Sub


    Private Sub CommandButton1_Click()
    Dim cel As Range
    For Each cel In Selection
    If Not Intersect(cel, Columns(1)) Is Nothing Then
             With cel
            .Interior.Color = vbYellow
            .Offset(, 1) = Date
            .NumberFormat = "ddd.mmm.yyyy"
            End With
       Else
       MsgBox "You can only select cells in column A"
       End If
      Next cel
    End Sub
    Last edited by david000; 03-27-2014 at 07:38 AM.
    "To a man with a hammer everything looks like a nail." - Mark Twain

  3. #3
    Ohhhhh, I like that idea.
    I've just given that a go and it's a very good solution for when I have more than 10 to check off.

    Thanks David !!

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    If Target.Interior.ColorIndex <> xlColorIndexNone Then
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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