Excel 如果列a中的单元格是唯一的,则更改代码以删除行

Excel 如果列a中的单元格是唯一的,则更改代码以删除行,excel,vba,Excel,Vba,我需要删除列A中具有唯一值的所有行 我从这根线上找到的 到 谢谢 我的尝试,它运行,但什么也没有发生 立即编辑作品 Sub DeleteUnique() Dim ws As Worksheet Dim i As Long, lRow As Long Dim delRange As range '~~> This is your sheet Set ws = Sheets("Sheet2") With ws '~~> Get the last row which has data

我需要删除列A中具有唯一值的所有行

我从这根线上找到的

谢谢

我的尝试,它运行,但什么也没有发生 立即编辑作品

Sub DeleteUnique()
Dim ws As Worksheet
Dim i As Long, lRow As Long
Dim delRange As range

'~~> This is your sheet
Set ws = Sheets("Sheet2")

With ws
'~~> Get the last row which has data in Col A
lRow = .range("A" & .Rows.Count).End(xlUp).Row

'~~> Loop through the rows
For i = 2 To lRow
    '~~> For for multiple occurances
    If .Cells(i, 1).Value <> "" Then
    If Application.WorksheetFunction.CountIf(.Columns(1), .Cells(i, 1)) = 1 Then
            '~~> Store thee row in a temp range
            If delRange Is Nothing Then
                Set delRange = .Rows(i)
            Else
                Set delRange = Union(delRange, .Rows(i))
            End If
        End If
    End If
Next
End With

'~~> Delete the range
If Not delRange Is Nothing Then delRange.Delete

End Sub
Sub-DeleteUnique()
将ws设置为工作表
暗我一样长,浅我一样长
范围
“~~>这是你的床单
设置ws=图纸(“图纸2”)
与ws
“~~>获取列A中包含数据的最后一行
lRow=.range(“A”&.Rows.Count).End(xlUp).Row
“~~>在行中循环
对于i=2至lRow
“~~>用于多次发生
如果.Cells(i,1).Value为“”,则
如果Application.WorksheetFunction.CountIf(.Columns(1),.Cells(i,1))=1,则
“~~>将行存储在临时范围内
如果delRange什么都不是,那么
设置delRange=.Rows(i)
其他的
Set delRange=Union(delRange,.Rows(i))
如果结束
如果结束
如果结束
下一个
以
“~~>删除范围
如果不是delRange,则delRange.Delete为Nothing
端接头
原件:Siddharth Rout

Option Explicit

Sub mukjizat2()
Dim ws As Worksheet
Dim i As Long, lRow As Long
Dim delRange As Range

'~~> This is your sheet
Set ws = ThisWorkbook.Sheets("process")

With ws
    '~~> Get the last row which has data in Col A
    lRow = .Range("A" & .Rows.Count).End(xlUp).Row

    '~~> Loop through the rows
    For i = 2 To lRow
        '~~> For for multiple occurances
        If .Cells(i, 2).Value <> "" And .Cells(i, 3).Value <> "" Then
            If Application.WorksheetFunction.CountIf(.Columns(2), .Cells(i, 2)) = 1 And _
            Application.WorksheetFunction.CountIf(.Columns(3), .Cells(i, 3)) = 1 Then
                '~~> Store thee row in a temp range
                If delRange Is Nothing Then
                    Set delRange = .Rows(i)
                Else
                    Set delRange = Union(delRange, .Rows(i))
                End If
            End If
        End If
    Next
End With

'~~> Delete the range
If Not delRange Is Nothing Then delRange.Delete
End Sub
选项显式
子mukjizat2()
将ws设置为工作表
暗我一样长,浅我一样长
范围
“~~>这是你的床单
设置ws=ThisWorkbook.Sheets(“流程”)
与ws
“~~>获取列A中包含数据的最后一行
lRow=.Range(“A”&.Rows.Count).End(xlUp).Row
“~~>在行中循环
对于i=2至lRow
“~~>用于多次发生
如果.Cells(i,2).Value“”和.Cells(i,3).Value“”,则
If Application.WorksheetFunction.CountIf(.Columns(2),.Cells(i,2))=1和_
Application.WorksheetFunction.CountIf(.Columns(3),.Cells(i,3))=1,则
“~~>将行存储在临时范围内
如果delRange什么都不是,那么
设置delRange=.Rows(i)
其他的
Set delRange=Union(delRange,.Rows(i))
如果结束
如果结束
如果结束
下一个
以
“~~>删除范围
如果不是delRange,则delRange.Delete为Nothing
端接头

它运行了,但什么也没发生
-试着在最后添加这一行:
如果不是delRange,那么delRange就什么都不是。删除
simoco,那一段代码是存在的,我在我的帖子中错过了。很抱歉。Tim,刚刚测试了这段代码-效果很好!如果您可以将链接发布到您的工作簿(例如,使用或其他文件上传程序),这会很有帮助。由于某种原因,这会把我搞得一团糟
Set ws=thiswoolk.Sheets(“Sheet2”)
我将其更改为
Set ws=Sheets(“Sheet2”)
,一切正常。非常感谢。我将更新
我的尝试
,以反映修复情况`
Sub DeleteUnique()
Dim ws As Worksheet
Dim i As Long, lRow As Long
Dim delRange As range

'~~> This is your sheet
Set ws = Sheets("Sheet2")

With ws
'~~> Get the last row which has data in Col A
lRow = .range("A" & .Rows.Count).End(xlUp).Row

'~~> Loop through the rows
For i = 2 To lRow
    '~~> For for multiple occurances
    If .Cells(i, 1).Value <> "" Then
    If Application.WorksheetFunction.CountIf(.Columns(1), .Cells(i, 1)) = 1 Then
            '~~> Store thee row in a temp range
            If delRange Is Nothing Then
                Set delRange = .Rows(i)
            Else
                Set delRange = Union(delRange, .Rows(i))
            End If
        End If
    End If
Next
End With

'~~> Delete the range
If Not delRange Is Nothing Then delRange.Delete

End Sub
Option Explicit

Sub mukjizat2()
Dim ws As Worksheet
Dim i As Long, lRow As Long
Dim delRange As Range

'~~> This is your sheet
Set ws = ThisWorkbook.Sheets("process")

With ws
    '~~> Get the last row which has data in Col A
    lRow = .Range("A" & .Rows.Count).End(xlUp).Row

    '~~> Loop through the rows
    For i = 2 To lRow
        '~~> For for multiple occurances
        If .Cells(i, 2).Value <> "" And .Cells(i, 3).Value <> "" Then
            If Application.WorksheetFunction.CountIf(.Columns(2), .Cells(i, 2)) = 1 And _
            Application.WorksheetFunction.CountIf(.Columns(3), .Cells(i, 3)) = 1 Then
                '~~> Store thee row in a temp range
                If delRange Is Nothing Then
                    Set delRange = .Rows(i)
                Else
                    Set delRange = Union(delRange, .Rows(i))
                End If
            End If
        End If
    Next
End With

'~~> Delete the range
If Not delRange Is Nothing Then delRange.Delete
End Sub