简体   繁体   中英

delete the strikethrough cells in a range using vba

I have 500 rows and around 13 columns of data in a sheet.

I need to delete the cell contents itself even if the cell contains all the characters as strike-through, but if the cell contains combination of some text and strike-through it should delete the strike-through alone and leave the remaining text in the cell.

Here is how my excel looks like

  A      B               C   D   E   F   G   H   I   J   K    L       M
 1.2   SERVER_P                  RE1                        **GR5** 
 7.3   PROXY NET
 4.5   NET **CON** V1                            GR

If text inside ** are strike-through I expect, in 1st row column L should be empty and in 3rd row it should delete CON , so it should remain "NET V1".

Here is what I have till now

Dim Cell As Range
Dim iCh As Integer
Dim NewText As String
Sheets("Copy_indications").Select

With ActiveSheet
     'count the rows till which strings are there
     Lrow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With

For Each Cell In Range("B1:M" & Lrow)
     For iCh = 1 To Len(Cell)
        With Cell.Characters(iCh, 1)
           If .Font.Strikethrough = False Then
              NewText = NewText & .Text
           End If
        End With
     Next iCh
 NewText = Cell.Value
 Cell.Characters.Font.Strikethrough = False
Next Cell

My macro deletes all the strike-through characters if the cell contains combination of some text and strike-through, but if the cell contains all the characters as strike-through, it does not delete them instead it removes the strike from them.

Could someone help me with this.

Good solution, just corrected a few mistakes (see comments in the code)

Dim Cell As Range, iCh As Integer, NewText As String

With Sheets("Copy_indications") ' <~~ avoid select as much as possible, work directly with the objects

    Lrow = .Cells(.Rows.Count, "B").End(xlUp).Row
    For Each Cell In .Range("B1:M" & Lrow)
        For iCh = 1 To Len(Cell)
             With Cell.Characters(iCh, 1)
                 If .Font.Strikethrough = False Then NewText = NewText & .Text
            End With
        Next iCh
        Cell.Value = NewText ' <~~ You were doing it the other way around
        NewText = "" ' <~~ reset it for the next iteration
        Cell.Characters.Font.Strikethrough = False
    Next Cell

End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM