Delete Cells with Errors
ฝัง
- เผยแพร่เมื่อ 31 ธ.ค. 2024
- Delete Cells with Errors.
This is the code:
Sub DeleteAllNACells()
Dim ws As Worksheet
Dim lastRow As Long
Dim col As Range
Dim cell As Range
' Set the worksheet you want to work on
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name
' Loop through columns A, B, and C
For Each col In ws.Range("A:D").Columns
' Find the last row in each column
lastRow = ws.Cells(ws.Rows.Count, col.Column).End(xlUp).Row
' Loop through each cell in the column from bottom to top
' Going from bottom to top prevents skipping rows after deletion
For i = lastRow To 1 Step -1
Set cell = col.Cells(i, 1)
' Check if the cell contains an error and if that error is #N/A
If IsError(cell.Value) Then
If cell.Value = CVErr(xlErrNA) Then
' Delete the cell and shift up
cell.Delete Shift:=xlUp
End If
End If
Next i
Next col
MsgBox "All #N/A cells have been deleted in columns A to D", vbInformation
End Sub