Excel VBA - Check if cell contains specific characters - excel

I'm using the following to check if a cell contains any invalid characters.
If Target Like "*[\!%^:~#|#.;`\/*$,]*" Then
This works fine and return the correct error message.
I'd like to add Double Quotes " to the check, but when I add " I get errors.
How do I do this ?
Thanks

Doubling the quotes should do it:
Like "*[\!%^:~#|#.;`\/*$,""]*"
Note that \ is the LIKE escape char, so "\!" means in fact "!".
Test:
a = "abc""de"
? a
abc"de
? a like "*[b]*"
True
? a like "*[""]*"
True

Related

Nested strings for text(,format) conversion

How do you use Text() with a format that has a string inside it ?
=TEXT(A1,"Comfi+"#0"(JO)";"Comfi-"#0"(JO)")
Tried """ both the inner string :
=TEXT(A1," """Comfi+"""#0"""(JO)""";"""Comfi-"""#0"(JO)""" ")
Same result with &char(34)&
Similar issue here, but I couldn't transpose the solution to my problem : How to create strings containing double quotes in Excel formulas?
Post Solution edit :
Building an almanac/calendar with the following (now fixed)formula :
=CONCATENATE(
TEXT(Format!K25,"d"),
" J+",
Format!S25,
" ",
TEXT(Format!AA25,"""Comfi+""#0""(JO)"";""Comfi-""#0""(JO)"""),
" ",
Format!AI25
)
Giving the following output in each cell :
9
J+70
Comfi+21(JO)
CRG
You've got too many quotation marks inside:
=TEXT(A1,"""Comfi+""#0""(JO)"";""Comfi-""#0""(JO)""")
You were tripling many of the inside quotation marks.
Personally, doubling up double-quotes within a quoted string is something I try to avoid at all costs. You can 'escape' the text into literals with a backslash.
=TEXT(A1,"\C\o\m\f\i+#0\(\J\O\);\C\o\m\f\i-#0\(\J\O\)")
'alternately
="Comfi"&text(a1, "+#0;-#0")&"(JO)"
Not all of those actually need to be escaped; only reserved characters. However, I usually escape them all and let Excel sort them out.

How do I delete text before the last forward slash in a column?

I have a spreadsheet with columns that show a filepath. They look like this:
/j/t/jtfdsrn-01r_1_1_19.jpg
/j/t/jtfdsrn-01r_1_1_18.jpg
/j/t/jtfdsrn-01r_1_1_17.jpg
/j/t/jtfdsrn-01r_1_1_16.jpg
/j/t/jtfdsrn-01r_1_1_15.jpg
/j/t/jtfdsrn-01r_1_1_14.jpg
/j/t/jtfdsrn-01r_1_1_13.jpg
/j/t/jtfdsrn-01r_1_1_12.jpg
I want to remove everything before the last slash so they look like this:
/jtfdsrn-01r_1_1_19.jpg
/jtfdsrn-01r_1_1_18.jpg
/jtfdsrn-01r_1_1_17.jpg
/jtfdsrn-01r_1_1_16.jpg
/jtfdsrn-01r_1_1_15.jpg
/jtfdsrn-01r_1_1_14.jpg
/jtfdsrn-01r_1_1_13.jpg
/jtfdsrn-01r_1_1_12.jpg
Can I do this with a formula or an built-in function? I use OpenOffice.
I have tried the TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",LEN(A1))),LEN(A1))) formula but I get a Error:501 on it.
If your target string is always the same length:
=RIGHT(A1,23)
Input: /j/t/jtfdsrn-01r_1_1_19.jpg Output: /jtfdsrn-01r_1_1_19.jpg
If you have variable length strings and always 3 backslashes in filepath:
="/" &RIGHT(A1, LEN(A1) -FIND("*", SUBSTITUTE(A1,"/","*",3), 1))
Input: /j/t/jtfdsrn-01r_1_1_1000.jpg Output: /jtfdsrn-01r_1_1_1000.jpg
If you have variable length strings and variable backslashes in filepath:
="/" &RIGHT(A1, LEN(A1) -FIND("*", SUBSTITUTE(A1,"/","*", LEN(A1)-LEN(SUBSTITUTE(A1,"/","") )), 1))
Input: a/b/c/j/t/jtfdsrn-01r_1_1_19.jpg Output: /jtfdsrn-01r_1_1_19.jpg
Let me know if this works for you if your values dont change there shouldnt be an issue but if so ill look into it.
=RIGHT(A2;LEN(A2)-FIND("/";A2;3)-1)
Simply Use Wildcard (*/) Without Brackets In Find and Replace

python3 replace ' in a string

I am trying to clean text strings containing any ' or &#39 (which includes an ; but if i add it here you will see just ' again. Because the the ANSI is also encoded by stackoverflow. The string content contains ' and when it does there is an error.
when i insert the string to my database i get this error:
psycopg2.ProgrammingError: syntax error at or near "s"
LINE 1: ...tment and has commenced a search for mr. whitnell's
the original string looks like this:
...a search for mr. whitnell&#39s...
To remove the ' and &#39 ; I use:
stripped_content = stringcontent.replace("'","")
stripped_content = stringcontent.replace("&#39 ;","")
any advice is welcome, best regards
When you try to replace("&#39 ;","") it literally searching for "&#39 ;" occurrences in string. You need to convert "&#39 ;" to its character equivalent. Try this:
s = "That's how we 'roll"
r = s.replace(chr(int('&#39'[2:])), "")
and with this chr(int('&#39'[2:])) you'll get ' character.
Output:
Thats how we roll
Note
If you try to run this s.replace(chr(int('&#39'[2:])), "") without saving your result in variable then your original string would not be affected.

Check to see if cell contains 2 spaces directly after each other

Is there a way that I can check to see if my cell contains 2 spaces immediately after one another?
For example if my cell contained "The cat went meow" then my formula below will return "No" because there is only one space between each character. However, if there where 2 spaces like
"The cat went meow"
then the formula would return "Yes".
I have tried the following formula but it picks up all the spaces instead of what I want it to do.
Can someone please show me how i could correct this?
=IF(ISNUMBER(SEARCH(" " & " ",B1)),"Yes","no")
Might be worth considering TRIM():
=LEN(B1)=LEN(TRIM(B1))
Test cases:
"The cat went meow" TRUE (single space)
"The cat went meow" FALSE (double space)
" The cat went meow" FALSE (leading space)
"The cat went meow " FALSE (trailing space)
this works for your example, if allowance is made for returning T/F rather than Y/N (to keep the formula short - Y/N could be arranged).
A better (shorter) version was offered by #Rick Hitchcock (to whom thanks) in a Comment:
=B1=TRIM(B1)
However it would return FALSE not only for "double" spaces but any quantity of spaces that are not on their own (immediately next to characters on both sides)
and
it would return FALSE even for single spaces if at the start and/or end of your string.
So this is not exactly what you asked for, and no more, but in practice seems likely to be more useful in general.
Your code works for me as-is. You could simplify it like this:
=IF(ISNUMBER(SEARCH(" ",B1)),"Yes","no")
To avoid error-checking, you could compare the string to a version with double-spaces converted to spaces, like this:
=IF(SUBSTITUTE(B1," "," ")<>B1,"Yes","no")
But I'm unsure what problem you're having with your existing code.
Try this
=IF(ISERROR(FIND(" ",B1,1)),"No","Yes")
or
=IF(ISERROR(SEARCH(" ",B1,1)),"No","Yes")

Is there a way to add quotes to a multi paragraph string

I wrote the following line:
string QuoteTest2 = "Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
This example went well, but what can I do in case I want to write a multi paragraph string including quotes?
The following example shows that puting '#' before the doesn't cut it..
string QuoteTest2 = #"Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
The string ends and the second quote and the over just gives me errors, what should I do?
Use double quotes to escape ""
e.g.
string QuoteTest2 = #"Benjamin Netnayahu,""BB"", said that: ""Israel will not fall""";

Resources