gpt4 book ai didi

excel - 如果它们等于某物,则删除最后 3 个字符

转载 作者:行者123 更新时间:2023-12-03 20:53:04 25 4
gpt4 key购买 nike

(不影响单元格中的字符级格式)

我已经发布了一个类似的问题,但不幸的是,我意识到我无法使用任何解决方案,因为它们都删除了单元格中的所有格式:(

我有一个替换各种文本字符串的代码。它看起来像这样(+ 更多的 IF):

Sub Fix()
Dim X As Long, Cell As Range
For Each Cell In Selection
For X = Len(Cell.Text) To 1 Step -1
If Cell.Characters(X, 3).Text = ", ," Then Cell.Characters(X, 3).Text = ","
If Cell.Characters(X, 3).Text = ", (" Then Cell.Characters(X, 3).Text = " ("
If Cell.Characters(X, 3).Text = ", [" Then Cell.Characters(X, 3).Text = " ["
If Cell.Characters(X, 3).Text = ", -" Then Cell.Characters(X, 3).Text = " -"
If Cell.Characters(1, 3).Text = "abc" Then Cell.Characters(1, 3).Text = ""
Next
Next
End Sub

我的代码的最后一行删除了 abc如果这包含在所选单元格的开头。

我正在寻找如何删除 abc何时包含 最后选定的单元格。

但对我来说,保留单元格中的所有原始格式(文本大小、颜色、粗体/斜体/下划线字母等)对我来说非常重要。

请记住,我是一个完整的初学者,我对编程一无所知。我从互联网上复制了上面的代码,只是更改了值。

预先感谢您的帮助。

最佳答案

替换字符(保留格式)

Option Explicit

Sub FixIt()

Const NoC As Long = 3
Const Special As String = "abc"
Dim Source As Variant: Source = Array(", ,", ", (", ", [", ", -")
Dim Target As Variant: Target = Array(",", " (", " [", " -")

Dim Cell As Range
Dim UB As Long: UB = UBound(Source)
Dim i As Long, x As Long
Dim Current As String

For Each Cell In Selection
Current = Cell.Characters(Len(Cell.Text) - NoC + 1, NoC).Text
If Current = Special Then _
Cell.Characters(Len(Cell.Text) - NoC + 1, NoC).Delete
For x = Len(Cell.Text) - NoC + 1 To 1 Step -1
GoSub replaceCurrent
Next
Next

Exit Sub

replaceCurrent:
Current = Cell.Characters(x, NoC).Text
For i = 0 To UB
If Current = Source(i) Then Current = Target(i): Exit For
Next
If i <= UB Then Cell.Characters(x, NoC).Text = Current
Return

End Sub

关于excel - 如果它们等于某物,则删除最后 3 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62075516/

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