大家好,菜鸟又回来了。我正在将数据从一个工作表复制粘贴到另一个隐藏的工作表中,但如果不检查已粘贴的内容,则存在重复数据的危险。到目前为止,我所做的是在我要复制到的工作表中插入一个代码,以停止重复,但我现在遇到的复杂情况是验证是从头到尾检查整个列中的每一位数据,这是大约 5000< 个条目。B 列的报告日期对于属于同一月底的所有条目都是相同的。所以它会说 5000 个条目,其中 30/1/13....另一个有 28/02/13 等。理想情况下,我只想在 B 列中检查一次输入报告日期的位置,以及日期是否与我想复制,然后拒绝整个复制粘贴过程,而不是验证复制范围中的每个单独条目。这是我正在使用的代码。我希望我说得通,非常感谢你的帮助。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Dim ans As String
Const myCol As Long = 2
If Intersect(Target, Columns(myCol)) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In Intersect(Target, Columns(myCol))
If Application.CountIf(Columns(myCol), r.Value) > 1 Then
MsgBox (r.Value & " already exsists")
r.ClearContents
End If
Next
Application.EnableEvents = True
End Sub
那是我的代码,包括删除重复项,但它不起作用。我试过了
Sub LoadData_toTable()
Dim ws1LRow As Long, ws2LRow As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Application.ScreenUpdating = False
Set ws1 = ThisWorkbook.Sheets("RAW DATA")
Set ws2 = ThisWorkbook.Sheets("DATA INPUT")
With ws1
ws1LRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row + 1
End With
With ws2
ws2LRow = .Range("G" & .Rows.Count).End(xlUp).Row
.Range("A2:AR" & ws2LRow).Copy
ws1.Range("A" & ws1LRow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End With
With ws1
ws1.Range("A:A").RemoveDuplicates
End With
For Each WS In ThisWorkbook.Worksheets
For Each PT In WS.PivotTables
PT.RefreshTable
Next PT
Next WS
MsgBox "Loading month's data complete!"
End Sub