Home » excel » vba – Deleting blank Cells and Cells containing Special Characters as # after Applying Trim Function in an entire column

vba – Deleting blank Cells and Cells containing Special Characters as # after Applying Trim Function in an entire column

Posted by: admin May 14, 2020 Leave a comment

Questions:

i hope u’r fine.

I have an ID Column containing some Blank Cells with spaces inside and other cells with information some of which with space & others with #

the aims is to apply TRIM function into the ID column and delete Blank Cells and Special Characters.
Rm : if I try to make deleting of the Blank cells before Applying Trim function VBA will not recognize them as empty cells. but even after Applying Trim function Still not recognize them as empty cells. Same Story with #
So I tried to make TRIM function and then copy Paste Column with only a value to delete TRIM Function in case. but same probleme. Tanks for your help

Here is the code

    Sub Trim()

 Dim Worksht As Worksheet
 Dim TargetCell As Range
 Dim DurtyRows As Range


 Set Worksht = ActiveSheet

 Set TargetCell = ActiveSheet.UsedRAnge.Find(What:="ID", LookAt:=xlWhole)

 Range(TargetCell.Offset(1, 0), TargetCell.Offset(1, 0).End(xlDown)).Copy
 TargetCell.Offset(1, 1).Select
 'To Apply TRIM Function in an copied column 
 ActiveSheet.Paste
 Application.CutCopyMode = False
 TargetCell.Offset(1, 1).Select

 ActiveCell.FormulaR1C1 = "=TRIM(RC[-1])"

 Selection.AutoFill Destination:=Range(TargetCell.Offset(1, 1), 
 TargetCell.Offset(1, 1).End(xlDown))

 'Replacing the initial Column with  TRIM Function Result Column

 Range(TargetCell.Offset(1, 1), TargetCell.Offset(1, 1).End(xlDown)).Copy

 TargetCell.Offset(1, 0).Select

 Selection.PasteSpecial Paste:=xlPasteValues

 Application.CutCopyMode = False

 Range(TargetCell.Offset(1, 1), TargetCell.Offset(1, 1).End(xlDown)).Delete

 Range(TargetCell.Offset(1, 0), TargetCell.Offset(1, 0).End(xlDown)).Select

 ''''the error message for.SpecialCells (xlCellTypeBlanks) " no corresponding  
 ''cell

 Set DurtyRows = ActiveSheet.Range(TargetCell.Offset(1, 0), TargetCell.Offset(1, 0).End(xlDown)).SpecialCells(xlCellTypeBlanks)

 DurtyRows.Delete

 End Sub
How to&Answers:

Try using Text-to-Columns to ‘trim’ the cells into truly blank cells after replacing #’s with a null string.

with worksheets("sheet1")
    with .range(.cells(2, "B"), .cells(.rows.count, "B").end(xlup))
        .replace what:=chr(35), replacement:=vbnullstring
        .texttocolumns  Destination:=.cells(1), _
                        DataType:=xlFixedWidth,  FieldInfo:=Array(0, 1)
        .SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
    end with
end with

Answer:

With Worksheets("trim")
    With .Range(TargetCell.Offset(1, 0), TargetCell.Offset(1, 0).End(xlDown))
    .TextToColumns Destination:=.Cells(1), _
                        DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)
        .Replace what:=Chr(35), replacement:=vbNullString
        .SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
    End With
End With