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
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'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.
Im programming with visual basic 2010 express. I have list of strings on format like "GOOD DAY" which have two words (on begining and on end) and 10 spaces between them. I want to get the words from that string. (I want to get GOOD and to get DAY). Which string operator may help me to success this easily. Thanks.
Trim removes leading, lagging, and consecutive spaces from strings.
Split splits a string by a delimiter of your choice.
Dim MyString
MyString = Split(Trim("Good Day"), " ")
MsgBox MyString(0)
MsgBox MyString(1)
I have created a script to convert text to morsecode, and now I want to modify it to include a slash between words.So something like space slash space between morsecode words. I know my loop before the main loop is incorrect and I want to fix it to do as stated before I just really need help Thank You!!!:
...
Word=input('Please enter a word:','s');
...
Code=MC_1;
...
case ' '
Code='/'
otherwise
Valid=0;
end
if Valid
fprintf('%s ',Code);
else
disp('Input has invalid characters!')
break
end
I know you want to write a loop to remove multiple spaces in between words, but the best way to remove white space in your particular problem would be to use regular expressions, specifically with regexprep. Regular expressions are used to search for particular patterns / substrings within a larger string. In this case, what we are trying to find are substrings that consist of more than one whitespace. regexprep finds substrings that match a pattern, and replaces them with another string. In our case, you would search for any substrings within your string that contain at least one more whitespace characters, and replace them with a single whitespace character. Also, I see that you've trimmed both leading and trailing whitespace for the string using strtrim, which is great. Now, all you need to do is callregexprep like so:
Word = regexprep(Word, '\s+', ' ');
\s+ is the regular expression for finding at least one white space character. We then replace this with a single whitespace. As such, supposing we had this string stored in Word:
Word = ' hello how are you ';
Doing a trim of leading and trailing whitespace, then calling regexprep in the way we talked about thus gives:
Word = strtrim(Word);
Word = regexprep(Word, '\s+', ' ')
Word =
hello how are you
As you can see, the leading and trailing white space was removed with strtrim, and the regular expression takes care of the rest of the spaces in between.
However, if you are dead set on using a loop, what you can do is use a logical variable which is set to true when we detect a white space, and then we use this variable and skip other white space characters until we hit a character that isn't a space. We would then place our space, then /, then space, then continue. In other words, do something like this:
Word = strtrim(Word); %// Remove leading and trailing whitespace
space_hit = false; %// Initialize space encountered flag
Word_noSpace = []; %// Will store our new string
for index=1:length(Word) %// For each character in our word
if Word(index) == ' ' %// If we hit a space
if space_hit %// Check to see if we have already hit a space
continue; %// Continue if we have
else
Word_noSpace = [Word_noSpace ' ']; %// If not, add a space, then set the flag
space_hit = true;
end
else
space_hit = false; %// When we finally hit a non-space, set back to false
Word_noSpace = [Word_noSpace Word(index)]; %// Keep appending characters
end
end
Word = Word_noSpace; %// Replace to make compatible with the rest of your code
for Character = Word %// Your code begins here
...
...
What the above code does is that we have an empty string called Word_noSpace that will contain our word with no extra spaces, and those spaces replaced with a single whitespace. The loop goes through each character, and should we encounter a space, we check to see if we have already encountered a space. If we have, just continue on in the loop. If we haven't, then concatenate a whitespace. Once we finally hit a non-space character, we simply just add those characters that are not spaces to this new string. The result will be a string with no extra spaces, and those are replaced with a single white space.
Running the above code after you trim the leading and trailing white space thus gives:
Word =
hello how are you
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); "-")