0
A
回答
0
存儲第一個實例的單元格供以後刪除。 然後去刪除重複,直到結束。
Dim F as integer, S as integer 'indices for First and Second cells to be compared
Dim Deleted as boolean 'indicates if second line was deleted
Dim First as Range, Second as Range 'First and second cells to be compared
Dim Start as string 'Indicates the position of the first cell's start
Start = "A1" 'can be as you like
Set First = Sheet1.Range(Start) 'Sets the start cell
F = 0 '
Do While First.Value <> "" 'loop while sheet contains data in the column
S = F + 1 'second cell is at least 1 cell below first cell
Deleted = false 'no second cell was deleted yet
Set Second = First.Offset(S,0) 'second cell is an offset of the first cell
Do While Second.Value <> "" 'loop while second cell is in sheet's range with data
if Second.Value = First.Value then 'if values are duplicade
Second.EntreRow.Delete 'delete second cell
Deleted = true 'stores deleted information
else 'if not, second cell index goes next
S = S + 1;
end if
Set Second = First.Offset(S, 0) 'sets second cell again (if deleted, same position, if not deleted, next position
Loop
if Deleted then 'if deleted, should delete first cell as well
First.EntireRow.Delete
else
F = F + 1 'if no duplicates found, first cell goes next
end if
Set First = Sheet1.Range(Start).Offset(F,0) 'sets first cell again (if deleted, same position, if not, next)
Loop
1
短一點的解決方案,以便快速上午的訓練做:
Sub quicker_Option()
Dim toDel(), i As Long
Dim RNG As Range, Cell As Long
Set RNG = Range("a1:a19") 'set your range here
For Cell = 1 To RNG.Cells.Count
If Application.CountIf(RNG, RNG(Cell)) > 1 Then
ReDim Preserve toDel(i)
toDel(i) = RNG(Cell).Address
i = i + 1
End If
Next
For i = UBound(toDel) To LBound(toDel) Step -1
Range(toDel(i)).EntireRow.Delete
Next i
End Sub
+0
非常有趣!我從來沒有想過使用數組來存儲單元地址。 (+1)的想法。 –
0
我使用此代碼創建總帳控制帳戶的自動對賬,如果用同等價值符號相反的任何小區是指切成片2;因此只留下對賬項目。
的代碼:
sub autoRecs()
dim i as long
Application.ScreenUpdating = False
Application.StatusBar = True
Dim i As Long
Cells(5, 6).Select
Dim x As Long
Dim y As Long
x = ActiveCell.Row
y = x + 1
Do Until Cells(x, 6) = 0
Do Until Cells(y, 6) = 0
Application.StatusBar = "Hey Relax! You can rely on me......"
If Cells(x, 6) = Cells(y, 6) * -1 Then
Cells(x, 6).EntireRow.Cut (Worksheets(2).Cells(x, 6).EntireRow)
Cells(y, 6).EntireRow.Cut (Worksheets(2).Cells(y, 6).EntireRow)
Cells(x, 6).Value = "=today()"
Cells(y, 6).Value = "=today()"
Else
y = y + 1
End If
Loop
x = x + 1
y = x + 1
Loop
Application.StatusBar = False
End Sub
Sub deleteBlankCells()`this is to delete unnecessary cells after run the above macro`
Range(Cells(5, 1), Cells(Rows.Count, 1).End(xlUp)).Select
For i = Selection.Rows.Count To 1 Step -1
Application.StatusBar = "OOH! I'm cleaning all the blanks for you....."
If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
Selection.Rows(i).EntireRow.Delete
End If
Next i
Application.StatusBar = False
End Sub
0
我喜歡內VBA陣列的工作,所以這裏是一個例子。
- 假設該數據表示圍繞A1的currentregion,但是,很容易改變
- 讀取源數據到一個數組
- 檢查在一列中的每個項目,以確保它是唯一的(即項目的COUNTIF = 1)
- 如果唯一,請將相應的行號添加到集合中
- 使用集合的大小和列數來調暗結果數組。
- 循環訪問集合,將相應的行寫入結果數組。
- 將結果數組寫入工作表。
正如所寫,結果放置在源數據的右側,但也可以替換它,或放置在不同的工作表上。
Option Explicit
Sub RemoveDuplicatedRows()
Dim vSrc As Variant, vRes() As Variant
Dim rSrc As Range, rRes As Range
Dim colUniqueRows As Collection
Dim I As Long, J As Long
'assume data starts in A1 and represented by currentregion
Set rSrc = Range("a1").CurrentRegion
vSrc = rSrc
Set rRes = rSrc.Offset(0, UBound(vSrc, 2) + 2)
'get collection of non-duplicated rows
Set colUniqueRows = New Collection
For I = 1 To UBound(vSrc)
If WorksheetFunction.CountIf(rSrc.Columns(1), vSrc(I, 1)) = 1 Then _
colUniqueRows.Add I
Next I
'Make up results array
ReDim vRes(1 To colUniqueRows.Count, 1 To UBound(vSrc, 2))
For I = 1 To UBound(vRes, 1)
For J = 1 To UBound(vSrc, 2)
vRes(I, J) = vSrc(colUniqueRows(I), J)
Next J
Next I
rRes.EntireColumn.Clear
rRes.Resize(UBound(vRes)) = vRes
End Sub
相關問題
- 1. VBA Excel刪除重複行
- 2. Excel VBA如何根據C列刪除所有重複的行
- 3. 刪除重複行(不要刪除所有重複)
- 4. vba刪除該行中所有使用的行和複選框?
- 5. 刪除mysql中除重複行之外的所有行
- 6. 刪除所有重複行記事本++
- 7. 刪除所有重複值
- 8. 刪除給定行的值重複VBA
- 9. 總和重複的行,然後刪除所有重複
- 10. 刪除行重複數據VBA
- 11. 刪除重複項excel vba
- 12. vba刪除新集中的重複項
- 13. 複雜的VBA刪除多個頁面的重複,然後刪除空白行循環所有表
- 14. 刪除重複的值的行的所有副本中的R
- 15. CSV刪除與重複的值的所有行中的一列
- 16. 有效刪除所有重複記錄
- 17. PowerShell - 刪除所有重複的條目
- 18. 刪除MySQL中的所有重複的行5.7.9
- 19. VBA刪除沒有列值的重複行
- 20. SQLite如何刪除除一個之外的所有重複行?
- 21. 使用C刪除文件中的所有重複行C
- 22. 從一個重複的數據表中刪除所有行
- 23. 刪除MySQL中所有某些重複條目的第一行
- 24. 如何在所有字段重複時刪除db2中的重複行?
- 25. 如何刪除所有沒有主鍵的重複行?
- 26. 刪除所有非重複數組php
- 27. 刪除所有非重複項(球拍)
- 28. Prolog刪除所有重複函數
- 29. 刪除所有重複列表成員
- 30. 刪除重複行
夢幻般的消息。你嘗試了什麼? – ApplePie
試試我的[重複主外接](http://sdrv.ms/18pQI6C)。如果需要,可以跨頁進行作業,處理區分大小寫,空白區域和正則表達式匹配。 – brettdj
看到這個答案:http://stackoverflow.com/a/37358305/6201755 – ib11