如果A2值与A1相同,则清除单元格A2的内容

时间:2016-07-20 04:02:42

标签: vba excel-vba excel

我在A列和B列中有不同值的列表,它们分别为几行输出相同的值。如下:

        BEFORE
  column A |   column B
1.   a             b
2.   a             b 
3.   a             b 
4.   a             b  
5.   z             z
6.   z             z
7.   z             z
8.   z             z

         AFTER
  column A |   column B
1.   a             b
2.                 
3.                 
4.                  
5.   z             z
6.                
7.                
8.             

如何删除同一列中前行单元格的重复单元格?就像After。

到目前为止,我已完成以下操作:

Sub clear()
Dim x
Dim c

x = 1
c = Range("a1").Value

Do Until Cells(x, 1) = ""
    If Cells(x, 1) = Cells(x + 1, 1) Then
        Cells(x + 1, 1) = Range().ClearContents  
    End If
Loop
End Sub

3 个答案:

答案 0 :(得分:1)

试试这个:

Option Explicit

Sub clear()
Dim cRow As Long
Dim CellValue As String

cRow = 2 ' start the loop in row 2
CellValue = Range("a1").Value

Do Until Cells(cRow, 1) = ""
    If Cells(cRow, 1) = CellValue Then
        Cells(cRow, 1).ClearContents
        Cells(cRow, 2).ClearContents ' clear column B cell
    Else
        CellValue = Cells(cRow, 1) ' when the cell value changes,
    End If
    cRow = cRow + 1 ' increment the row number so the next loop goes to the next row
Loop
End Sub

我更喜欢带有xc的描述性名称的变量。

您的代码存在以下问题:

  • 范围必须有一个参数,但是当你已经拥有Cell()
  • 的单元格对象时,你不需要Range
  • 您没有增加设置行的变量
  • 清除的单元格成为新的当前单元格,因为它刚刚被清除,循环结束
  • 您从未使用c变量,但保留比较字符串的记录非常有用。

答案 1 :(得分:0)

您可能需要考虑以下"数组"方法应该是一个问题:

Option Explicit

Sub main()
    Dim i As Long, j As Long
    Dim myArr As Variant, refVal As Variant

    With Worksheets("MySheet") '<--| change "MySheet" with your actual sheet name
        With Intersect(.UsedRange, .Columns("A:B")) '<--| consider only columns A and B rows down to the worksheet used range last one
            myArr = .Value ''<--|  store values in array
            For j = LBound(myArr, 2) To UBound(myArr, 2) '<--| loop through array columns
                refVal = myArr(1, j) '<--| get column fiurst reference value
                For i = LBound(myArr, 1) + 1 To UBound(myArr, 1) '<--| loop through form current column 2nd row downwards
                    If myArr(i, j) = refVal Then '<--| if there's a duplicate...
                        myArr(i, j) = "" '<--| ...erase it
                    Else '<--| otherwise...
                        refVal = myArr(i, j) '<--| ... get the new non duplicate value as the reference one
                    End If
                Next i
            Next j
            .Value = myArr '<--| write back array to worksheet
        End With
    End With
End Sub

使用Option Explicit语句是一种安全的习惯,以一些额外的工作为代价来声明所有变量可以让您更好地控制代码,更少的调试和维护问题

使用全范围参考(例如Worksheets("MySheet").Range(...))是一个很好的习惯,可以避免因用户选择页面而导致的问题

答案 2 :(得分:0)

为了补充teylyn和user3598756的答案,您可以从下到上检查值:

Public Sub myClear(Optional ByRef wks As Worksheet = Nothing)
    Dim c As Range
    Dim col As Long
    If wks Is Nothing Then Set wks = ActiveSheet
    For col = 1 To 2   'Columns A and B
        Set c = wks.Cells(wks.Rows.Count, col).Rows.End(xlUp)
        Do While c.Row > 1
            If c.Value = c.Offset(-1, 0).Value Then c.ClearContents
            Set c = c.Offset(-1, 0)
        Loop
    Next col
End Sub

默认情况下,子文件会在ActiveSheet上使用,但您可以在参数中输入您想要处理的实际工作表。