Is it possible to remove the " character using an Excel formula?
I tried the REPLACE function like this
REPLACE(<sometext>, """, "")
and this
REPLACE((<sometext>, char(34), "")
but it doesn't work.
NB: I am using the Polish version of Excel, so it is probable, that the syntax is different than in the English version. For example, in Polish formulas we use ; instead of , as argument separator.
The quotation mark character " must be represented by two of them "" when it's inside a string, to revolve the ambiguity between this "textual" quotation mark and the "code" quotation marks that delimit the beginning and end of your string.
In short, the right formula to remove all " from the text in A1 is:
=SUBSTITUTE(A1,"""","")
with emphasis on the fact that the old_text argument has to be """" (four quotes) and not """ (three quotes) as you wrote in your question.
And maybe SUBSTITUTE is called Replace in the Polish edition? Anyhow, you have to use the Polish equivalent of SUBSTITUTE and whatever argument separator is appropriate (; or ,).
Replace doesn't work the way, you want it to.
Use SUBSTITUTE.
=SUBSTITUTE(A1, CHAR(34), "-")
OR use ; as separator for your example
=SUBSTITUTE(A1; CHAR(34); "-")
Related
I need to replace non-printable characters with double quotation "
The problem is, this bad character Chr(25) can come after number, single time or twice and even comes after number and double quotation "
If I used excel clean function ,that will remove all Chr(25) and not replace it.
Range("C2") = WorksheetFunction.Clean(Range("B2"))
I also tried to use vba Replace function, but again the problem is count and position of Non-Printable Characters:
Range("C2") = Replace(Range("B2"), Chr(25) & Chr(25), """")
'If Chr(25) is single, this code will replace and add again
In advance, grateful for all your help.
I'd suggest a regular expression to catch and replace these characters:
Function RegexReplace(s As String) As String
With CreateObject("vbscript.regexp")
.Pattern = "(\d+)[""']*\u0019+"
.Global = True
RegexReplace = .Replace(s, "$1""")
End With
End Function
See an online demo of the pattern which means:
(\d+) - Capture any 1+ digits in a capture group;
["']* - Match 0+ double/single quotes;
\u0019+ - Match 1+ 'END OF MEDIUM' characters.
Replace with $1" means to input the captured digits followed by a double quote.
Formula in B1:
=RegexReplace(A1)
Note: If you don't need to specify the digits, you could leave (\d+) out and just use ["']*\u0019+ with a simple replacement of a single ".
I am searching for a way to use a formatter to put a space between two characters. i thought it would be easy with a string formatter.
here is what i am trying to accomplish:
given: "AB" it will produce "A B"
Here is what i have tried so far:
"AB".format("%#s")
but this keep returning "AB" i want "A B". i thought the number sign could be used for space.
i also tried this:
"26".format("%#d") but its still prints "26"
is there anyway to do this with string.formatter.
It is kind of possible with the string formatter although not directly with a pattern.
jshell> String.format("%1$c %2$c", "AB".chars().boxed().toArray())
$10 ==> "A B"
We need to turn the string into an object array so it can be passed in as varargs and the formatter pattern can extract characters based on index (1$ and 2$) and format them as characters (c).
A much simpler regex solution is the following which scales to any number of characters:
jshell> "ABC^&*123".replaceAll(".", "$0 ").trim()
$3 ==> "A B C ^ & * 1 2 3"
All single characters are replaced with them-self ($0) followed by a space. Then the last extra space is removed with the trim() call.
I could not find way to do this using String#format. But here is a way to accomplish this using regex replacement:
String input = "AB";
String output = input.replaceAll("(?<=[A-Z])(?=[A-Z])", " ");
System.out.println(output);
The regex pattern (?<=[A-Z])(?=[A-Z]) will match every position in between two capital letters, and interpolate a space at that point. The above script prints:
A B
I m trying to:
Replace double spaces with single.
Clean
Trim
the same string and i use:
AmountI = Replace(Application.WorksheetFunction.Clean(Trim(.Cells(j, 9).Value)), " ", " ")
I want to know if there is a specific order of those three VBA functions or the order does not play any role in the outcome.thanks for the help!
vba.trim doesn't remove double spacing, only leading/trailing spaces. You need the worksheet version Application.Trim or WorksheetFunction.Trim to remove interim double spaces.
If you have triple spacing, a single Replace will leave you with a double space. Application.Trim will not.
You don't want Clean to process characters (i.e. spaces) that you are going to remove anyways so Trim first.
.Value2 (without currency or date information) is marginally faster processing when you don't have currency or date data; you have a string so use .Value2.
AmountI = Application.Clean(Application.Trim(.Cells(j, 9).Value2))
AmountI = Trim(Replace(Application.WorksheetFunction.Clean(.Cells(j, 9).text), " ", " "))
My logic is:
1 .Text is faster than .Value
2 Clean may close up two spaces by removing a non-printing char between them, so do this before...
3 Turn double spaces to single with replace, then
4 Remove leading and trailing spaces
I'm trying to prepare my data to be inserted in a SQL database, so certain pieces need to be surrounded by quotes.
For Each r In Range("A2", last_data)
r.Value2 = """ & r.Value2 & """
Next r
This just populates everything with 'r.value2'. Using a single quote makes it only appear on the right side of the word.
Essentially, the sheet needs to be modified so every word is surrounded by double quotes. I had a custom code in Excel that surrounded everything with quotes but when VBA reads the cells it ignores the custom formatting.
I think you need to escape the single, double quote within the ... er... quotes:
For Each r In Range("A2", last_data)
r.Value2 = """" & r.Value2 & """"
Next r
The fact that the escape character is, unfortunately, yet another double quote character, makes things a bit confusing.
So for each double quote that you want to add, you have an escape character ("), followed by the actual double quote that you want to add. These are then surrounded by two more double quotes, denoting that it is a string value.
I'm sorry if I haven't explained this well, but I hope you get the idea.
Although, as #Comintern suggests in comments, there is most likely a better way of doing what you want.
I'm using excel's vba to match some numerical ratings. The rating can include a suffix:
+ add .25 to the rating
- subtract .25 from the rating
" add .50 to the rating
So for example if the rating: 5" would really mean 5.5 or 5+ would mean 5.25
I have a simple conditional:
if ActiveCell.Characters(i,1).Text Like [123456789+-"] then ...
Unfortunately the " produces an invalid character error. I've tried escaping as follows:
"""
'"'
\"
chr(34)
but can't seem to get the code to work. Thus, my question: How do you match a single double quote using excel VBA's Like function.
Use double quotes around Like, i.e.:
...Like "[-123456789+""]"...
Basically, when using Like, the regex is wrapped in double quotes and the double quote inside it is escaped with a second one.
Note that the - (hyphen) needs to be at the start or end of the regex, otherwise it denotes a range of characters. Thanks #ssarabando !