将数据复制到没有重复的另一张纸
我有一个主表(Sheet1),该表具有产品数据库(名称,参考,到期日期,状态,...)。
我的目标是根据一定条件(产品的状态)将数据复制到两张纸(Sheet3& Sheet4)。
每次打开Excel文件时,我的代码都会运行(因为状态可以根据到期日期更改)。
第一次打开文件时,我按计划获得复制数据。保存文件,然后关闭。
当我打开文件并在另一个时间执行代码时,复制相同的数据。因此,我得到了重复。
我添加了一些内容以删除重复项,但是代码运行缓慢。
Dim StatusCol As Range
Dim Status As Range
Dim PasteCell As Range
Dim DuplicateValues As Range
Dim DuplicateValues2 As Range
Set StatusCol = Sheet1.Range("G2:G999999")
For Each Status In StatusCol
If Sheet3.Range("A2") = "" Then
Set PasteCell = Sheet3.Range("A2")
Else
Set PasteCell = Sheet3.Range("A1").End(xlDown).Offset(1, 0)
End If
If Status = "about to expire" Then Status.EntireRow.Copy PasteCell
If Sheet4.Range("A2") = "" Then
Set PasteCell = Sheet4.Range("A2")
Else
Set PasteCell = Sheet4.Range("A1").End(xlDown).Offset(1, 0)
End If
If Status = "expired" Then Status.EntireRow.Copy PasteCell
Next Status
Set DuplicateValues = Sheet3.Range("A1:XFD1048576")
Set DuplicateValues2 = Sheet4.Range("A1:XFD1048576")
DuplicateValues.removeduplicates Columns:=Array(1), Header:=xlYes
DuplicateValues2.removeduplicates Columns:=Array(1), Header:=xlYes
End Sub
我试图添加一个条件,该条件验证数据是否已经存在于要制作副本的表中;因此,仅复制新数据。
Dim StatusCol As Range
Dim Status As Range
Dim Code As Range
Dim CodeCol As Range
Dim Code2 As Range
Dim CodeCol2 As Range
Dim PasteCell As Range
Dim DuplicateValues As Range
Dim DuplicateValues2 As Range
Set StatusCol = Sheet1.Range("G2:G999999")
Set CodeCol = Sheet3.Range("D2:D999999")
Set CodeCol2 = Sheet4.Range("D2:D999999")
For Each Status In StatusCol
If Sheet3.Range("A2") = "" Then
Set PasteCell = Sheet3.Range("A2")
Else
Set PasteCell = Sheet3.Range("A1").End(xlDown).Offset(1, 0)
End If
If Status = "about to expire" Then
With CodeCol
Set Code = .Find(What:=CodeCol.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Code Is Nothing Then
Else
Status.EntireRow.Copy PasteCell
End If
End With
End If
If Sheet4.Range("A2") = "" Then
Set PasteCell = Sheet4.Range("A2")
Else
Set PasteCell = Sheet4.Range("A1").End(xlDown).Offset(1, 0)
End If
If Status = "expired" Then
With CodeCol2
Set Code2 = .Find(What:=CodeCol2.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Code2 Is Nothing Then
Else
Status.EntireRow.Copy PasteCell
End If
End With
End If
Next Status
End Sub
我没有收到任何错误,但是副本不会发生。
I have a main sheet (Sheet1) that has a database of products (name, reference, expiration date, status, ...).
My goal is to copy data to two sheets (Sheet3 & Sheet4) depending on a certain condition (the status of the products).
My code is run every time the Excel file is opened (because the statuses can change depending on the expiration date).
First time the file is opened, I get copied data as planned. The file is saved, then closed.
When I open the file and the code is executed another time, the same data is copied. Hence I get duplicates.
I added something to remove duplicates but the code runs slowly.
Dim StatusCol As Range
Dim Status As Range
Dim PasteCell As Range
Dim DuplicateValues As Range
Dim DuplicateValues2 As Range
Set StatusCol = Sheet1.Range("G2:G999999")
For Each Status In StatusCol
If Sheet3.Range("A2") = "" Then
Set PasteCell = Sheet3.Range("A2")
Else
Set PasteCell = Sheet3.Range("A1").End(xlDown).Offset(1, 0)
End If
If Status = "about to expire" Then Status.EntireRow.Copy PasteCell
If Sheet4.Range("A2") = "" Then
Set PasteCell = Sheet4.Range("A2")
Else
Set PasteCell = Sheet4.Range("A1").End(xlDown).Offset(1, 0)
End If
If Status = "expired" Then Status.EntireRow.Copy PasteCell
Next Status
Set DuplicateValues = Sheet3.Range("A1:XFD1048576")
Set DuplicateValues2 = Sheet4.Range("A1:XFD1048576")
DuplicateValues.removeduplicates Columns:=Array(1), Header:=xlYes
DuplicateValues2.removeduplicates Columns:=Array(1), Header:=xlYes
End Sub
I tried to add a condition that verifies whether the data already exists in the sheet where the copy is to be made; so only new data is copied.
Dim StatusCol As Range
Dim Status As Range
Dim Code As Range
Dim CodeCol As Range
Dim Code2 As Range
Dim CodeCol2 As Range
Dim PasteCell As Range
Dim DuplicateValues As Range
Dim DuplicateValues2 As Range
Set StatusCol = Sheet1.Range("G2:G999999")
Set CodeCol = Sheet3.Range("D2:D999999")
Set CodeCol2 = Sheet4.Range("D2:D999999")
For Each Status In StatusCol
If Sheet3.Range("A2") = "" Then
Set PasteCell = Sheet3.Range("A2")
Else
Set PasteCell = Sheet3.Range("A1").End(xlDown).Offset(1, 0)
End If
If Status = "about to expire" Then
With CodeCol
Set Code = .Find(What:=CodeCol.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Code Is Nothing Then
Else
Status.EntireRow.Copy PasteCell
End If
End With
End If
If Sheet4.Range("A2") = "" Then
Set PasteCell = Sheet4.Range("A2")
Else
Set PasteCell = Sheet4.Range("A1").End(xlDown).Offset(1, 0)
End If
If Status = "expired" Then
With CodeCol2
Set Code2 = .Find(What:=CodeCol2.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Code2 Is Nothing Then
Else
Status.EntireRow.Copy PasteCell
End If
End With
End If
Next Status
End Sub
I receive no error, but the copy doesn't occur.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
查找
或后续如果
语句正在引起问题,我会与以下相似的情况有所帮助:With the assumption that the
Find
or subsequentIf
statement are causing the issues, I would something similar to the below may help: