删除 Excel 中的单元格

delete cell in Excel

我有一个 excel table,如果三列具有相同的数据,我想删除第二个和第三个。我可以写 4. 数据相同或不同的列,但我不能先设置 1. 2. 或 3. 列数据

=IF((AND(A1=B1;B1=C1));"Same";"Different")

a   b   c

a   b   b

a   a   a

a   b   c

a   b   b

a   

您可以将其放入新模块中,但宏将 运行 放在 Active Sheet:

 Sub DeleteCells()
 Dim intResponse, i As Integer

 i = 1

 intResponse = MsgBox("Are you sure you want " & _
 "to delete the cells?", vbYesNo)

 If intResponse = vbYes Then
 Do While Cells(i, 1).Value <> "" 'does it one by one going down in the first column while the cell is not empty


    If (Cells(i, 1) = Cells(i, 2)) And (Cells(i, 1) = Cells(i, 3)) Then
    Cells(i, 2).Value = ""
    Cells(i, 3).Value = ""
    End If
    i = i + 1


 Loop

 End If
End Sub

可以使用公式,但比 VBA 更乏味。例如,在 E1 中复制到 G1,然后 E1:G1 复制下来以适应:

=IF(COUNTIF($A1:$C1,A1)<>3,A1,IF(COLUMN()=5,A1,""))

然后复制、选择性粘贴、顶部的值并删除列 A:D。

或者标记三个值相同的行(比如将 =COUNTIF(A1:C1,A1) 复制下来以适应)然后将该列过滤为 select 3 并清空其中的值ColumnsB:C(header 行除外)。