提问人:Connor Rebodos 提问时间:11/11/2023 最后编辑:Tim WilliamsConnor Rebodos 更新时间:11/11/2023 访问量:46
我正在创建一个实时数据库,该数据库为进程添加时间戳。重复条目的时间戳未放在正确的行中
I am creating a live database that timestamps a process. Timestamps of duplicate entries are not going in the correct row
问:
[在此图像中,当输入相同的序列号时,当输入重复条目时,时间戳不会进入原始条目的行。我正在尝试解决这个问题。该代码确实按预期删除了重复的条目,但未将时间戳放在正确的位置。
这是当前代码。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim cell As Range
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
' Define the range where you want to remove duplicates (Column A in this case)
Set KeyCells = Range("A:A")
' Check if the change occurred in the specified range
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
Application.EnableEvents = False
On Error Resume Next
For Each cell In KeyCells
If Not IsEmpty(cell.Value) Then
If Not dict.Exists(cell.Value) Then
dict(cell.Value) = cell.Row ' Store the row of the original entry
Else
' Clear contents only if the current row is not the same as the original
If cell.Row <> dict(cell.Value) Then
cell.ClearContents
End If
End If
End If
Next cell
On Error GoTo 0
Application.EnableEvents = True
End If
' Add timestamps in the same row
Dim ws As Worksheet
Dim rFound As Range
' Define the range where the serial numbers are located
Dim serialNumberColumn As Integer
serialNumberColumn = 1 ' Assuming serial numbers are in Column A, change if they are in a different column
' Set the range where the serial numbers exist (adjust "A1:A1000" to your actual range)
Set KeyCells = Range("A1:A1000")
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
' Assuming serial numbers are unique and in column A
Set rFound = Columns(serialNumberColumn).Find(What:=Target.Value, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFound Is Nothing Then
' Set the worksheet
Set ws = rFound.Worksheet
' Find the next empty cell after the last non-empty cell in the row from W to AE (Column 23 to 31)
Dim nextEmptyColumn As Integer
nextEmptyColumn = GetNextEmptyColumn(ws.Cells(rFound.Row, 23).Resize(, 9))
' If we found an empty column, we set the timestamp
If nextEmptyColumn <> 0 Then
Application.EnableEvents = False
ws.Cells(rFound.Row, nextEmptyColumn).Value = Now
Application.EnableEvents = True
End If
End If
End If
End Sub
Function GetNextEmptyColumn(rng As Range) As Integer
Dim cell As Range
GetNextEmptyColumn = 0
For Each cell In rng
If IsEmpty(cell.Value) Then
GetNextEmptyColumn = cell.Column
Exit For
End If
Next cell
End Function
答:
0赞
Tim Williams
11/11/2023
#1
我认为您可以简化:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range, c As Range, m, stamped As Boolean
If Target.CountLarge > 1 Then Exit Sub 'not handling multi-cell updates...
If Target.Column <> 1 Then Exit Sub 'column A only
If Len(Target.Value) = 0 Then Exit Sub 'nothing to update
'range you want to check (exclude empty rows below data)
Set KeyCells = Me.Range("A1:A" & Me.Cells(Rows.Count, "A").End(xlUp).Row)
'check if there's already a row above with the same serial#
m = Application.Match(Target.Value, KeyCells, 0)
If m < Target.Row Then Target.ClearContents 'if Target is a duplicate, clear it...
For Each c In Me.Rows(m).Cells(23).Resize(1, 9).Cells
If Len(c.Value) = 0 Then
c.Value = Now
stamped = True 'flag as stamped
Exit For
End If
Next c
If Not stamped Then MsgBox "No empty cell for timestamp!", vbExclamation
End Sub
注意:在工作表代码模块中指的是工作表,所以你不需要做任何类似的事情Me
Set ws = rFound.Worksheet
评论