excel 如何删除删除线而不改变颜色,粗体,斜体设置?

w6lpcovy  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(229)

我有部分删除线的单元格。为了更容易阅读,我想删除它。
我找到了这个VBA,但它不保留颜色或粗体字符。
你能帮我吗?
PS:所有带文本的单元格均以'开头

Sub DelStrikethroughText()
    Dim xRg As Range, xCell As Range
    Dim xStr As String
    Dim I As Long
    On Error Resume Next
    Set xRg = Application.InputBox("Please select range:", "KuTools For Excel", Selection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    Application.ScreenUpdating = Fase
        For Each xCell In xRg
            If IsNumeric(xCell.Value) And xCell.Font.Strikethrough Then
                xCell.Value = ""
            ElseIf Not IsNumeric(xCell.Value) Then
                For I = 1 To Len(xCell)
                    With xCell.Characters(I, 1)
                        If Not .Font.Strikethrough Then
                            xStr = xStr & .Text
                        End If
                    End With
                Next
                xCell.Value = xStr
                xStr = ""
            End If
        Next
    Application.ScreenUpdating = True
End Sub
qmelpv7a

qmelpv7a1#

您无法在不丢失任何每字符格式的情况下替换单元格中带有部分格式的值。
如果需要删除文本,请使用Characters()方法。

Sub DelStrikethroughText()
    Dim xRg As Range, xCell As Range
    Dim xStr As String
    Dim i As Long, v
    
    On Error Resume Next
    Set xRg = Application.InputBox("Please select range:", "KuTools For Excel", Selection.Address, , , , , 8)
    On Error GoTo 0
    If xRg Is Nothing Then Exit Sub
    
    Application.ScreenUpdating = False
    For Each xCell In xRg
        v = xCell.Value
        If IsNumeric(v) And xCell.Font.Strikethrough Then
            xCell.Value = ""
        ElseIf Not IsNumeric(v) Then
            For i = Len(v) To 1 Step -1
                With xCell.Characters(i, 1)
                    If .Font.Strikethrough Then
                        .Text = "" 'remove the character
                    End If
                End With
            Next
        End If
    Next
    Application.ScreenUpdating = True
End Sub

相关问题