admin管理员组文章数量:1400533
Below is code that works to add one !
to one or more selection of cells but how do I add one subsequent !
and then remove !!
after?
There are two sequences:
shift1<
toshift1!<
toshift1!!<
back toshift1<
shift1<^
toshift1!<^
toshift1!!<^
back toshift1<^
'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
Below is code that works to add one !
to one or more selection of cells but how do I add one subsequent !
and then remove !!
after?
There are two sequences:
shift1<
toshift1!<
toshift1!!<
back toshift1<
shift1<^
toshift1!<^
toshift1!!<^
back toshift1<^
'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
Share
Improve this question
edited Mar 24 at 17:40
braX
11.8k5 gold badges22 silver badges37 bronze badges
asked Mar 24 at 17:39
Mohamad BachroucheMohamad Bachrouche
15112 bronze badges
3
|
3 Answers
Reset to default 3Your two cases are essentially the same. It doesn't matter if there is a ^
after <
if you only change things before the <
.
Try:
If InStr(ActiveCell.Value, "!") = 0 Then
Selection.Cells.Replace "<", "!<"
ElseIf InStr(ActiveCell.Value, "!!<") > 0 Then
Selection.Cells.Replace "!!<", "<"
ElseIf InStr(ActiveCell.Value, "!<") > 0 Then
Selection.Cells.Replace "!<", "!!<"
End If
You didn't declare your "c". After declaring your "c', you will be able to fill in your data.
Other than that, i would put the condition test separately as function for better presentation.
the code is shown below, hope this can help you.
-------------------
I saw that you asked "indefinite "!"", in other answer, it can be handle in condition 4 below, replacing all "!" before adding it if all other condition did not meet.
---------------------
Sub ABC()
Dim CellA As Range
For Each CellA In Selection.Cells
CellA = ABC1(CellA)
Next CellA
End Sub
Function ABC1(StringA As Variant)
If InStr(1, StringA, "!!<") <> 0 Then
StringA = Replace(StringA, "!!<", "<")
ElseIf InStr(1, StringA, "!<") <> 0 Then
StringA = Replace(StringA, "!<", "!!<")
ElseIf InStr(1, StringA, "<") > 0 And InStr(1, StringA, "!<") = 0 Then
StringA = Replace(StringA, "<", "!<")
ElseIf InStr(1, StringA, "<") = 0 Then
StringA = Replace(StringA, "!", "")
StringA = StringA & "!"
End If
ABC1 = StringA
End Function
Be careful about mixing ActiveCell
and Selection.Cells
the way you have in your question and in @cybernetic.nomad's answer. Changing values in the whole selection based on the value of one cell could easily lead to wrong results. Consider these cells where A1 is the ActiveCell
, and A1:C1 are the Selection.Cells
. The result is shown in row 2; Bar!!<
becomes Bar!!!<
because of the initial value of Foo<
.
A | B | C | |
---|---|---|---|
1 before | Foo< | Bar!!< | Baz!< |
2 after | Foo!< | Bar!!!< | Baz!!< |
You can however, use this to your advantage, and solve your problem with this two-statement subroutine.
Public Sub Rotate()
Selection.Cells.Replace "<", "!<"
Selection.Cells.Replace "!!!<", "<"
End Sub
本文标签: excelReplace special characters within a string in selectionStack Overflow
版权声明:本文标题:excel - Replace special characters within a string in selection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744236321a2596571.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"" to "!" to "!!" back to ""
"shift" to "shift!" to "shift!!" back to "shift"
"shift<" to "shift!<" to "shift!!<" back to "shift<"
"shift<^" to "shift!<^" to "shift!!<^" back to "shift<^"
"shift^" to "shift!^" to "shift!!^" back to "shift^"
– Mohamad Bachrouche Commented Mar 25 at 18:13