【问题标题】:Excel VBA remove Duplicates using forExcel VBA 使用 for 删除重复项
【发布时间】:2021-11-27 08:12:39
【问题描述】:

我正在尝试删除每列的重复项 EX) 删除了 A1:A100、columns=1 / B1:B10-、colums=2......的重复项……以此类推

粗体代码不起作用 请帮帮我

Sub DeleteDulpicates()

Dim i As Integer
Dim X As Integer
Dim Endrow As Long
Dim Endcolumn As Long
Dim sht As Worksheet

Set sht = ActiveSheet

Endrow = Cells.SpecialCells(xlCellTypeLastCell).Row
Endcolumn = sht.Cells.SpecialCells(xlCellTypeLastCell).Column


For i = 1 To Endcolumn
    
***Range(sht.Cells(i, 1), sht.Cells(i, 15000)).RemoveDuplicates Columns:=i, Header:=xlNo***
Next
    
End Sub

【问题讨论】:

  • 从文档看来,您需要一个列索引数组,因此Columns:=Array(i) 可能会修复该错误。 (也 Endrow 不限定工作表(即 sht. 丢失。docs.microsoft.com/en-us/office/vba/api/…
  • @Tragamor:对于一列,一个数字就可以了。对于多列,它必须是1. 变体2. 从零开始的数组,如果您不想对其进行硬编码(例如Array(1, 2)),则必须评估3.。查看this example

标签: excel vba


【解决方案1】:

Cells 中有错误的行和列,如果要从单个列中删除重复项,Columns 参数应该是 1。

Option Explicit

Sub DeleteDulpicates()
Dim sht As Worksheet
Dim i As Long
Dim Endcolumn As Long
Dim Endrow As Long

    Set sht = ActiveSheet

    Endrow = Cells.SpecialCells(xlCellTypeLastCell).Row
    Endcolumn = sht.Cells.SpecialCells(xlCellTypeLastCell).Column

    For i = 1 To Endcolumn
        With sht
            .Range(.Cells(1, i), .Cells(Endrow, i)).RemoveDuplicates Columns:=1, Header:=xlNo
        End With
    Next i

End Sub

【讨论】:

  • 谢谢你救了我的命......我的救世主............我的天使......我爱你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
相关资源
最近更新 更多