gpt4 book ai didi

vba - 使用vba删除范围内的删除线单元格

转载 作者:行者123 更新时间:2023-12-04 21:57:51 26 4
gpt4 key购买 nike

我在一张表中有 500 行和大约 13 列数据。

即使单元格包含删除线的所有字符,我也需要删除单元格内容本身,但如果单元格包含某些文本和删除线的组合,它应该单独删除删除线并将剩余文本留在单元格中.

这是我的excel的样子

  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

如果 ** 内的文本是删除线,我希望在第一行 L 列应该是空的,在第三行它应该删除 CON ,所以它应该保持“NET V1”。

这是我到目前为止所拥有的
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

如果单元格包含某些文本和删除线的组合,我的宏会删除所有删除线字符,但如果单元格包含所有字符作为删除线,它不会删除它们,而是从它们中删除删除线。

有人可以帮我解决这个问题。

最佳答案

很好的解决方案,只是纠正了一些错误(见代码中的注释)

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

关于vba - 使用vba删除范围内的删除线单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40955977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com