Excel Escape 2 "" - excel

& CHAR(10) & REPT(" ", 20)& "5. Change object BusinessCardRequest(FirstName=$CreatedFor/FirstName;LastName=$CreatedFor/Surname;EmailAddress=$CreatedFor/Email;MobileNumber=$CreatedFor/Mobile;PositionTitle=if $PositionFirst != empty then $PositionFirst/Title else "" "";Brand=if $PositionFirst != empty then getCaption($PositionFirst/Brand) else "" "")"
I have above code in excel and I want to escape so that I show 2 empty string as value for If condition. But it gives error I tried using 3 like """ """ it is also not working. But when I remove one it works.
How to correctly escape 2 " " in excel

The correct way to escape a " in Excel is to add one more " before it.
By escaping a " you are asking Excel to treat it as literal text.
For example:
"The ""fox"" jumped over the lazy dog"
would evaluate as
The "fox" jumped over the lazy dog
In answer to your example below:
But it gives error I tried using 3 like """ """ it is also not working. But when I remove one it works.
You don't have to encase the problematic character in escape characters, you only need to immediately precede the problematic character with the escape character.

Related

Changing multiple ROW values with in a single column, Followed but a filtering process

So my goal here is to filter for 2 service code(there will be hundreds in a single column) but in this case I need "4" and "CO4" that is the letter o capitalized not the number zero. FYI
Issues/Goals:
4 and CO4 have a space in them like CO4(space) this varies as in some may not have the space. Humans..am I right? lol
Filtering in an addition column called 'Is Void' for False values with the above two service codes.* this is where I believe my issue is
2a) this is because I lose a lot of data about 1700 rows with that code I will show in a bit.
Sample Data base:
My code: This has everything imported and data base in open too.
dfRTR = dfRTR[["Zone Name", "End Time", "Is Void", "Ticket Notes", "Service Code", "Unit Count", "Disposal Monitor Name", "Addr No","Addr St", "Ticket Number"]] #Columns I will use
dfRTR.replace("CO4 ","CO4") #Takes out (space) in CO4
dfRTR.replace("4 ", "4") #Takes out (space) in 4
filt = dfRTR[(dfRTR['Is Void'] == False) & (dfRTR["Service Code"].isin(["CO4 ", "4"]))] #my problem child.
If I take this code out I have all my data, but with it only about 700-800 Rows which is supposed to be around 1500-2000k Rows in the column "Is Void".
I have only been coding for about two months, not knowing how to replace two values at once in the same column, is another topic. I am trying to automate my whole audit which can take 4hrs to 2-3days depending on the project. Any advice is greatly appreciated.
EDIT:
So, if i manual make my exccel all text then run this code:
dfRTR['Service Code'] = dfRTR['Service Code'].str.replace(r'\W', "")
filt = dfRTR[(dfRTR['Is Void'] != True) & dfRTR["Service
Code"].isin(["CO4","4"])]
filt.to_excel('VecTest.xlsx')
And I can return all my data I need filtered. Only down side is that my date will have text formatting. I will try to automate one column in this case 'Service Code' to be text then run it again.
Edit part 2: Making the above file in a CVS mas the filtering process way easier. Problem is converting it back to an excel. Excel formulas have an issue with simple formula like =A1=B1 if both cells A1 and B1 have a value of 1 they will not match. CVS pulls away all the extra "Formatting" but in the Excel VBA code I use to format it make the cell give off this warning, even though the data being pulled is from CVS format.
VBA code makes the values appear with the excel warning:
"The number in this cell is formatted as a text or preceded with an apostrophe"
Conclusion:
I would need to make all my check using python before using CVS formatting.
I figured it out. My solution is a function but it does not need to be:
def getRTRVecCols(dfRTR):
# this is majority of special chars that may be in columns cells
spec_chars = ["!", '"', "#", "%", "&", "'", "(", ")",
"*", "+", ",", "-", ".", "/", ":", ";", "<",
"=", ">", "?", "#", "[", "\\", "]", "^", "_",
"`", "{", "|", "}", "~", "–"]
# this forloop will remove all those special chars for a specific column
for char in spec_chars:
dfRTR["Service Code"] = dfRTR["Service Code"].str.replace(char, ' ')
#with the above we may get white spaces. This is how to remove those
dfRTR["Service Code"] = dfRTR["Service Code"].str.split().str.join(" ")
# take in multiple values for in one column to filter for (specific for me)
codes = ["CO4", "4"]
#filtering our data set with multiple conditions. 2 conditions is in the same single column and another condition in a differnt column
dfRTR = dfRTR[(dfRTR["Is Void"] != True) & (dfRTR["Service Code"].isin(codes))]
#saves to a excel file
dfRTR.to_excel('RTR Vec Cols.xlsx')
#my function call
getRTRVecCols(dfRTR)
Now I Do get this warning, which I am still to noob in python to understand yet. But is next on my list to fix. But it works perfect for now*
FutureWarning: The default value of regex will change from True to False in a
future version. In addition, single character regular expressions will*not* be treated as literal strings when regex=True.
dfRTR["Service Code"] = dfRTR["Service Code"].str.replace(char, ' ')

I have text in separate lines inside same box, How to make it all in one line

Some of my text are in different lines inside same cell. I want them in single line. How do I bring them in single line ?
Example:
first cell contains:
Hi Ram, I want to go to movie today.
Are you willing to join?
If yes, let me know early.
Example:
Expected output:
Hi Ram, I want to go to movie today.Are you willing to join?If yes, let me know early.
New line in a cell A1 caused by alt+Enter for example, may be removed using a formula such as:
=SUBSTITUTE(A1,CHAR(10)," ")
Where A1 is the cell containing the text to be changed. You can enter the formula above in a different cell of course.
The parameter " " indicates 1 space to replace the line break. You could use any other character.
Another type of line break is CHAR(13). You can remove CHAR(13) using the same function again:
=SUBSTITUTE(SUBSTITUTE(A1, CHAR(13)," "), CHAR(10), " ")
In case you had some spaces already before the new-line character, you need to wrap the above formula in a TRIM function like so:
=TRIM(SUBSTITUTE(A1,CHAR(10)," "))
OR
=TRIM(SUBSTITUTE(SUBSTITUTE(A1,CHAR(13)," "),CHAR(10)," "))
Always make a copy of your file before you apply formulas that could change the data.
Note-1:
char(13) is officially called "carriage return" and char(10) is called "line feed".
CHAR(10) returns a line break on Windows, and CHAR(13) returns a line break on the Mac. This answer is for Windows. You can't visually see it but you can see its effect.
Note-2:
As #kojow7 answered, a text wrap can cause the text to appear on more than 1 line depending on the cell width and the text length. This answer does not resolve this case.
Related discussion can be found here: Remove line breaks from cell.
Two things you may need to fix here: 1) Line breaks and 2) Text Wrapping
To fix line breaks:
Select the cells that need to be changed
Press CTRL+H to open Search and Replace
In the Find box type CTRL+J to insert the line break character (it may look like nothing was inserted in the field, but it does insert a line break)
Decide whether to replace the line breaks with a space or with nothing
Press Replace All
To turn off text wrapping:
Select the cells that need to be changed
Go to the Home Tab
In the Alignment Group check to see if the Wrap Text button is clicked.
If it is, click on it again to deselect it.
Depending on your situation, you may need to fix either one or both of these.
Depending on your document it might contain linefeeds or carriage returns or BOTH.
Alexander Frolov (https://www.ablebits.com/office-addins-blog/2013/12/03/remove-carriage-returns-excel/) has written a very good blog post about different technics of finding and removing linebreaks in an Excel file. We will use the “macro way” of doing that – as it is the one that works either on Windows AND Mac. The search replace method offered here too will not work on Mac but on windows.
Add the below Macro to your document (slighlty modified from the original)
Change the value of “ReplaceWith” from ” ” (space) to anything you like a linebreak to be replaced with.
E.g. ReplaceWith = “-” will result in “Line1-Line2-Line3”
Run the Macro (Extras > Macro) while all cells are selected.
Sub RemoveCarriageReturns()
ReplaceWith = " "
LinefeedChar = Chr(10)
Dim recordRange As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each recordRange In ActiveSheet.UsedRange
If 0 < InStr(recordRange, LinefeedChar) Then
recordRange = Replace(recordRange, LinefeedChar, ReplaceWith)
End If
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
If your separate lines are not gone by now please change "LinefeedChar" from "Chr(10)" to "Chr(13)" and run it again

Issues with Range.FormulaLocal and textstring in formula

When I finally had figured out that I can use my own language with Range.FormulaLocal in stead of Range.Formula I was very excited!
The possibilities are endless. But, I am encountering a problem with textstrings in formulas.
This code works just fine:
Range("I5").FormulaLocal = "=ALS(A5=1;H5;0)"
But these codelines are not working:
Range("I5").FormulaLocal = "=ALS(A5="x";H5;0)"
Range("I6").FormulaLocal = "=ALS.FOUT(VERT.ZOEKEN(A2;'betaaltermijnen.xlsx'!tabel;3;ONWAAR);"")
Could somebody help me?
You're accidentally ending your strings early...
First line:
If you have a variable x which you want to include in the string, then use &
Range("I5").FormulaLocal = "=ALS(A5=" & x & ";H5;0)"
If instead you're trying to have the string "x" then you must use an additional quotation mark before each in-string quotation mark. This is called an escape character.
Range("I5").FormulaLocal = "=ALS(A5=""x"";H5;0)"
This way, when VBA sees "", it treats it as the start or end of a quote within a string
By the same reasoning, your second line becomes
Range("I6").FormulaLocal = _
"=ALS.FOUT(VERT.ZOEKEN(A2;'betaaltermijnen.xlsx'!tabel;3;ONWAAR);"" "") "
Where I've used the _ underscore to continue the line without it getting too long, because the last 6 characters are the important bit!

Separate words with commas in Excel 2010

I'm trying to use a formula in Excel to separate a bunch of words in a cell with a comma. If there are more than 5 words in the cell, I just want to get the first 5 words. To get the first five words in a cell and separate them by a comma I use this:
=SUBSTITUTE(LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",5))-1), " ", ", ")
This works fine. But the problem with this, because of the number 5 here, if I a cell contains less than 5 words, I get an error. I tried to substitute the 5 with this:
LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1
So my function becomes this:
=SUBSTITUTE(LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1))-1), " ", ", ")
But this doesn't work, it gives me an error. Any idea how I can do this please?
Also I would like to ignore the first word if its first character is "-" (without the quotes) and just start from the second word. So in other words, I want something like this:
I love my life very much should return I, love, my, life, very
- I love my life very much should return I, love, my, life, very (the "-" is ignored")
I love my should return I, love, my
Thanks in advance for any help
Here's a somewhat different approach. Aside from the "less than 5" issue, it also deals with the "5 words with no space at the end" issue:
=LEFT(A1,FIND("^",SUBSTITUTE(A1 & "^"," ","^",5))-1)
EDIT 1: I just noticed the part about the leading "- ". My addition isn't very elegant, but it deals with it, and also TRIMS any trailing spaces:
=TRIM(LEFT(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1),FIND("^",SUBSTITUTE(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1) & "^"," ","^",5))-1))
EDIT 2: Oh yeah, commas:
=SUBSTITUTE(TRIM(LEFT(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1),FIND("^",SUBSTITUTE(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1) & "^"," ","^",5))-1))," ",",")
Try this:
=TRIM(LEFT(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-"," "))," ",","),",",REPT(" ",99),5),99))
This will work even if there is not a space after the dash or if there are extra spaces in the text. Often I find that input is not very clean.
=SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-","",1)),
" ","*",5),IFERROR(FIND("*",SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-","",1)),
" ","*",5))-1,999))," ",",")
Edit: After commenting on István's, I made mine flawless too.
=SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(LEFT(TRIM(A1),1),"-"," ",1)
&MID(TRIM(A1),2,999))," ","*",5),IFERROR(FIND("*",SUBSTITUTE(
TRIM(SUBSTITUTE(LEFT(TRIM(A1),1),"-","",1)&MID(TRIM(A1),2,999))," ","*",5))-1,999))," ",",")
But I think his is more elegant.
Try this:
=SUBSTITUTE(LEFT(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "),", ","|",MIN(LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "))-LEN(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", ")," ","")),5)),FIND("|",SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "),", ","|",MIN(LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "))-LEN(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", ")," ","")),5)))-1),",,",",")
The formula works by taking the following steps:
Remove any leading dash-space
Trim any leading or trailing spaces
Insert comma-spaces in place of spaces and add a trailing comma-space
Calculate the lesser of 5 and the number of words in the string
Put in "|" in place of either the fifth comma-space or the trailing comma-space if the string is less than five words
Determine the position of the "|"
Strip off the "|" and all characters to the right of it
Remove any doubled commas due to any single embedded commas in the initial string
If you are willing to consider a VBA solution, this complex expression can be replaced by a user-defined function:
Function words5(InputString As String) As String
Dim wordArray As Variant
wordArray = Split(Trim(Replace(InputString, _ 'remove "-", put words into array
"-", "", , 1)), " ")
ReDim Preserve wordArray(LBound(wordArray) To _ 'drop all but the first 5 words
WorksheetFunction.Min(UBound(wordArray), 5 - 1))
words5 = Replace(Join(wordArray, ", "), ",,", ",") 'rejoin the words with ", "
End Function 'separator
On the plus side of using this code is its maintainability compared to the worksheet formula, which impossible to understand or safely alter without access to the original building blocks that were combined into the single expression.
The code would have to be installed in the workbook in which it is used or in either the standard Personal.xlsb workbook or an addin workbook.
To use the function, copy and paste it into a standard module, which can be inserted into a workbook via the VBA editor. You can open the editor with the Visual Basic button on the `Developer tab of the ribbon.
Figured I'd throw my hat in the ring also. I think this formula should cover the bases:
=SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(A1&" ","- ",""))," ",REPT(" ",99)),99*5))," ",",")

In VBA for Excel, How can I manipulate a header/footer when the string contains '&'

I have a section of code that writes to the header section of the page. The problem is that the string that is written to the header occasionally contains a '&' symbol. This interferes with the code because VBA automatically views & and the following character as a piece of code, even if its part of the string.
ActiveSheet.PageSetup.CenterHeader = "&B&14 Cool New Header for &16 &I" & vbNewLine & aString
Everything works great when aString has no '&' symbol in it. I get a bold, size 14 top line followed by the size 16, italicized aString text on a second line.
In the event that aString is something like 'B&S Company of Greatness' the header will come out with "Company of Greatness" stricken out because of the '&S' contained within the string.
How can I get around this? I'd do a search in string for '&S and compensate manually by inserting another &S to cancel it out but &S might not be the only occurance of the '&' symbol that occurs.
What's the best way to get around this? Would properly diming things help by telling excel to read aString ONLY as a string value and to not apply its contents improperly?
You need to escape the ampersand by adding a second ampersand to each instance in the string, like this:
aString = Replace(aString, "&", "&&")
ActiveSheet.PageSetup.CenterHeader = "&B&14 Cool New Header for &16 &I" & _
vbNewLine & aString

Resources