Consulting

Results 1 to 5 of 5

Thread: Creating a logging column and fill out missing values

  1. #1
    VBAX Newbie
    Joined
    Apr 2024
    Posts
    3
    Location

    Creating a logging column and fill out missing values

    Hi there! I am trying to create a script where when I select a value in a cell (column D) from a dropdown, then the cell next to it in column E, will populate with the selection, the current user, and the date time. It works except for when you autofill or copy paste the value. In order to address that, I created a script that should check if column D is populated and if column E is not, then then it should be filled. This only works after I manually initiate the function. I need it to work as soon as you open excel. Here is the entire script. I have been stuck on this problem all day and I just can't think clearly about it anymore. Any help is greatly appreciated.

        Sub FillMissingInColumnE()
            Dim ws As Worksheet
            Dim lastRow As Long, i As Long
            Dim newValue As String
    
    
            Set ws = ActiveSheet
        
            lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
            
            For i = 1 To lastRow
                If ws.Cells(i, "D").Value <> "" And ws.Cells(i, "E").Value = "" Then
                    newValue = ws.Cells(i, "D").Value & " | " & ws.Cells(i, "D").Value & _
                               " by " & Environ("username") & " on " & _
                               Format(Now, "yyyy-mm-dd HH:mm:ss")
                    ws.Cells(i, "E").Value = newValue
                End If
            Next i
        
        End Sub
    
    
        Private Sub Worksheet_Change(ByVal Target As Range)
            Dim Cell As Range
            Dim AffectedCells As Range
            Dim IntersectRange As Range
            
            Set IntersectRange = Intersect(Target, Me.Columns("D"))
            
            If Not IntersectRange Is Nothing Then
                For Each Cell In IntersectRange
                    If Len(Cell.Value) > 0 Then
                        Set AffectedCells = Cell.Offset(0, 1)
                        
                        AffectedCells.Value = AffectedCells.Value & " | " & Cell.Value & " by " & _
                                              Environ("username") & " on " & _
                                              Format(Now, "yyyy-mm-dd HH:mm:ss")
                    End If
                Next Cell
        
                FillMissingInColumnE
            End If
            
            Application.EnableEvents = True
        End Sub

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Welcome Spyfly to VBAX. When I tried to debug your code it objected to the use of Me.Columns("D"). When I removed the "Me." it compiled.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Newbie
    Joined
    Apr 2024
    Posts
    3
    Location
    Looks like this worked.

    I don't understand VBA very well, but wouldn't it make more sense to embed the missingcolumns function into the worksheetchange function?

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Well we could i guess, but I was a little concerned when you suggested that you wanted it to occur on opening the workbook.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Newbie
    Joined
    Apr 2024
    Posts
    3
    Location
    Anyway it works! Thank you very much!

Tags for this Thread

Posting Permissions

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