我编写了一个宏,它对我的表进行排序,并删除重复的行,如下所示:
Entitydocnum Docstatus Purchase-order Created-date Eyepeak
======================================================================
test1 pending EL351-EE 27/03/2017 2
test2 pending EL351-EE 06/04/2017 0
test1 pending EL351-EE 30/03/2017 0
test4 pending EL351-EE 25/03/2017 2正如您所看到的,'test1‘行是重复的,因为the认为它因为日期不同而不同,这是不一样的。2017年3月30日有一行“test1”,另一行是2017年3月27日。
如何使宏忽略创建的列-date(只有此列)将test1 (27/03/2017)与test1 (30/03/2017)合并。其中的日期值较高..。?
此时此刻,我的宏是:
(我的桌子从“B3”开始)
Sub thepcshop_macrotest()
ActiveSheet.Range("B3").Sort _
Key1:=ActiveSheet.Columns("B"), _
Header:=xlGuess
Do While Not IsEmpty(ActiveCell) ' Tant que la cellule active n'est pas vide, recommence
If ActiveCell = ActiveCell.Offset(-1, 0) Then ' Condition : si la cellule active est identique
ActiveCell.EntireRow.Delete ' ˆ la cellule prŽcŽdente (mme colonne), supprime
Else: ActiveCell.Offset(1, 0).Select 'toute la ligne. Sinon, passe ˆ la cellule suivante.
End If
Loop
MsgBox "Done :)"
End Sub发布于 2017-04-13 12:24:10
您可以将数据按降序排序,然后根据前三列删除重复项。
Sub thepcshop_macrotest()
Dim rData As Range 'Whole data range
Dim rDocNum As Range 'EntityDocNum range
Dim rCreated As Range 'Created-date range
With ThisWorkbook.Worksheets("Sheet1") 'Sheet name will need updating.
'Reference required data ranges - many ways of doing this.
'This method will work if there's nothing else on sheet.
Set rData = .Range(.Cells(Rows.Count, 2).End(xlUp), .Cells(3, Columns.Count).End(xlToLeft))
Set rDocNum = .Range(.Cells(4, 2), .Cells(Rows.Count, 2).End(xlUp))
Set rCreated = .Range(.Cells(4, 5), .Cells(Rows.Count, 5).End(xlUp))
'Sort by DocNum ascending and Created date descending.
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=rDocNum, _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=rCreated, _
SortOn:=xlSortOnValues, _
Order:=xlDescending, _
DataOption:=xlSortNormal
With .Sort
.SetRange rData
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Remove duplicates based on EntityDocNum, DocStatus and Purchase-order.
rData.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
End With
End Sub发布于 2017-04-13 11:44:48
检查这段代码是否符合您的要求。
Sub thepcshop_macrotest()
Dim Tmp As Variant
ActiveSheet.Range("B3").Sort Key1:=ActiveSheet.Columns("B"), _
Header:=xlGuess
Do While Not IsEmpty(ActiveCell) ' Tant que la cellule active n'est pas vide, recommence
With ActiveCell
If .Value = .Offset(-1, 0).Value Then ' Condition : si la cellule active est identique
Tmp = .Offset(0, 3).Formula
If Tmp <> .Offset(-1, 3).Value Then ' i the previous is different
Tmp = Application.Max(Tmp, .Offset(-1, 3).Value)
' replace the previous with the current if it is more recent
If Tmp < .Offset(-1, 3).Value Then .Offset(-1, 3).Value = Tmp
End If
.EntireRow.Delete ' ? la cellule pr?c?dente (mme colonne), supprime
Else
.Offset(1, 0).Select 'toute la ligne. Sinon, passe ? la cellule suivante.
End If
End With
Loop
MsgBox "Done :)"
End Sub使用简单的语言:如果选定的单元格与上面的单元格具有相同的值,它将检查D列中的日期。如果当前行中的日期是最近的,则将更改宝贵行中的日期。无论测试的结果如何,如果当前行在A列中的值与上面的行相同,则该行将被删除。
https://stackoverflow.com/questions/43390045
复制相似问题