我在VBA中有一些代码正在尝试删除重复的交易ID。但是,我想修改代码以仅删除具有交易ID的重复项-因此,如果没有交易ID,我希望将该行保留下来。这是我的代码如下:
With MySheet
newLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
newLastCol = .Cells(5 & .Columns.Count).End(xlToLeft).Column
Set Newrange = .Range(.Cells(5, 1), .Cells(newLastRow, newLastCol))
Newrange.RemoveDuplicates Columns:=32, Header:= _
xlYes
End With
我还想知道-在remove.duplicates命令中,是否有一种方法可以让我希望查看的列被命名,而不是在以后添加或删除列的情况下为32?
这是数据的图像:我想要ExchTransID列,该3个空格可以单独放置。
答案 0 :(得分:0)
修改并尝试以下操作:
Option Explicit
Sub test()
Dim Lastrow As Long, Times As Long, i As Long
Dim rng As Range
Dim str As String
'Indicate the sheet your want to work with
With ThisWorkbook.Worksheets("Sheet1")
'Find the last row with IDs
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Set the range with all IDS
Set rng = .Range("A1:A" & Lastrow)
'Loop column from buttom to top
For i = Lastrow To 1 Step -1
str = .Range("A" & i).Value
If str <> "" Then
Times = Application.WorksheetFunction.CountIf(rng, str)
If Times > 1 Then
.Rows(i).EntireRow.Delete
End If
End If
Next i
End With
End Sub