下面的代码可用于将一个单元格添加!
到一个或多个单元格选择中,但是如何添加一个后续单元格然后!
删除!!
呢?
有两个序列:
shift1<
回到shift1!<
shift1!!<
shift1<
shift1<^
回到shift1!<^
shift1!!<^
shift1<^
'successfully adds ! to any selection of blank cells or after any string
'example: from "blank cell" to "!" or from "shift1" to "shift1!"
If InStr(ActiveCell.Value, "!") = 0 And InStr(ActiveCell.Value, "^") = 0 Then
If InStr(ActiveCell.Value, "<") = 0 And InStr(ActiveCell.Value, ">") = 0 _
And InStr(ActiveCell.Value, "^") = 0 Then
For Each c In Selection.Cells
c.Value = c.Value & "!"
Next c
'successfully adds ! in between shift string and < if ! is not present
'example: from "shift1<" to "shift1!<"
ElseIf InStr(ActiveCell.Value, "!") = 0 Then
Selection.Cells.Replace "<", "!<"
'does not add another ! in between shift string and < if one ! is present
'example: from "shift1!<" to "shift1!!<"
ElseIf InStr(ActiveCell.Value, "!") = 1 Then
Selection.Cells.Replace "!<", "!!<"
'does not remove !! in between shift string and < if two ! are present
'example: from "shift1!!<" to "shift1<"
ElseIf InStr(ActiveCell.Value, "!") = 2 Then
Selection.Cells.Replace "!!<", "<"
End If
'successfully adds ! in between shift string and <^
'example: from "shift1<^" to "shift1!<^"
ElseIf InStr(ActiveCell.Value, "<") > 0 And InStr(ActiveCell.Value, "^") > 0 _
And InStr(ActiveCell.Value, "!") = 0 Then
Selection.Cells.Replace "<^", "!<^"
'does not add another ! in between shift string and <^
'example: from "shift1!<^" to "shift1!!<^"
ElseIf InStr(ActiveCell.Value, "<") > 0 And InStr(ActiveCell.Value, "^") > 0 _
And InStr(ActiveCell.Value, "!") = 1 Then
Selection.Cells.Replace "!<^", "!!<^"
'does not remove !! in between shift string and < if two ! are present
'example: from "shift1!!<^" to "shift1<^"
ElseIf InStr(ActiveCell.Value, "<") > 0 And InStr(ActiveCell.Value, "^") > 0 _
And InStr(ActiveCell.Value, "!") = 2 Then
Selection.Cells.Replace "!!<^", "<^"
End If
您的两个案例本质上是相同的。如果您只更改之前的内容,那么是否有
^
之后的内容并不重要。<
<
尝试:
在您的问题和@cybernetic.nomad 的回答中,请注意混合
ActiveCell
和Selection.Cells
使用的方式。根据一个单元格的值更改整个选择中的值很容易导致错误的结果。考虑这些单元格,其中 A1 是ActiveCell
,而 A1:C1 是Selection.Cells
。结果显示在第 2 行;由于 的初始值,Bar!!<
变为。Bar!!!<
Foo<
然而,您可以利用这一点,用这个双语句子程序来解决您的问题。