VBA移除重複資料
Excel有個實用的功能,可以把重複的資料快速移除,那要怎麼使用VBA「移除重複」呢?
如將以下重複的姓名移除。
![](https://lazyorangelife.com/wp-content/uploads/2023/11/VBA移除重複資料-1.png)
輸入巨集
※輸入以下指令※
Sub 移除重複資料()
Range(“A1”).CurrentRegion.RemoveDuplicates _
Columns:=1, Header:=xlYes
End Sub
![](https://lazyorangelife.com/wp-content/uploads/2023/11/VBA移除重複資料-2.png)
語法說明
Range(“A1”).CurrentRegion
‘選取「A1儲存格」向右與向下到空白欄與空白列邊界的範圍,也就是「選取」目前區域儲存格
RemoveDuplicates Columns:=1
針對第「1」欄重複資料進行移除
Header:=xlYes
是否包含標題列,否為「xlNO」(預設值);是為「xlYes」
執行巨集
點選「開發人員」-「巨集」或按下「Ctrl」+「F8」快捷鍵,並「執行」巨集。
![](https://lazyorangelife.com/wp-content/uploads/2023/11/VBA移除重複資料-3.png)
執行後則針對第「1」欄重複資料進行移除,即將姓名重複之資料移除。
![](https://lazyorangelife.com/wp-content/uploads/2023/11/VBA移除重複資料-4.png)
移除其他欄
※輸入以下指令※
Sub 移除重複資料()
Range(“A1”).CurrentRegion.RemoveDuplicates _
Columns:=3, Header:=xlYes
End Sub
![](https://lazyorangelife.com/wp-content/uploads/2023/11/VBA移除重複資料-5.png)
將「Columns」改成3,則針對第「3」欄重複資料進行移除,即將捐血日期重複之資料移除。
![](https://lazyorangelife.com/wp-content/uploads/2023/11/VBA移除重複資料-6.png)