Home » excel » delete cell in Excel

delete cell in Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a excel table and I want to delete if three column have same data delete second and third. I can write 4. column which datas same or different but I cannot set first 1. 2. or 3. column data

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

a   b   c

a   b   b

a   a   a

to

a   b   c

a   b   b

a   
How to&Answers:

You can put this into a new module, but the macro will run on the 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

Answer:

With a formula is possible but a little more tedious than VBA. For example in E1 copied across to G1 and then E1:G1 copied down to suit:

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

Then Copy, Paste Special, Values over the top and delete Columns A:D.

Alternatively flag the rows where the three values are the same (say with =COUNTIF(A1:C1,A1) copied down to suit) then filter that column to select 3 and blank out the values in ColumnsB:C (other than the header row).