我想根据 Excel 表格中的三列删除重复行。 当我将列作为直接值传递给数组(例如第 1、61 和 122 列)时,它可以正常工作,如下面的代码所示:
cur.Range("data[#All]").RemoveDuplicates Columns:=Array(1, 61, 122), Header:=xlYes
但是当我尝试通过在标题中查找列号来动态传递数组中的列值时,它既不会抛出任何错误,也不会删除重复项 下面的代码是用于传递列的动态代码
Set employeeCell = cur.Range("1:1").Find(What:="employee id")
Set customerCell = cur.Range("1:1").Find(What:="customer")
Set dateCell = cur.Range("1:1").Find(What:="date")<br/>
cur.Range("data[#All]").RemoveDuplicates Columns:=Array(employeecell.column,customercell.column, datecell.column), Header:=xlYes
请帮忙解决这个问题。我想通过查找各自的列号来动态删除基于三列的重复项。
最佳答案
尝试使用ListColumn.Index
每列:
Dim dataTable as ListObject
Set dataTable = cur.ListObjects("data")
Dim employeeIndex as Long
employeeIndex = dataTable.ListColumns("employee id").Index
Dim customerIndex as Long
customerIndex = dataTable.ListColumns("customer").Index
Dim dateIndex as Long
dateIndex = dataTable.ListColumns("date").Index
dataTable.Range.RemoveDuplicates Columns:=Array(employeeIndex, customerIndex, dateIndex), _
Header:=xlYes
关于excel - VBA Excel-如何根据三列删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59286694/