I have URL formatted content, usually I just translate manually 1-by-1m but this time there are thousand of entry, eg:-
%E5%B7%B2%E4%BB%8E%E5%B8%90%E6%88%B7zh*****%40outlook.com%E5%88%A0%E9%99%A48618650533*%E3%80%82%E4%B8%8D%E6%98%AF%E4%BD%A0%EF%BC%9F
%E7%AE%A1%E7%90%86%E9%A2%84%E8%AE%A2%0A
https%3A%2F%2Faccount.live.com%2Fa
OTO+GLOBAL+Certification+No%3A%5B6198%5D
Deluxe+Room+-1+%E9%97%B4%0A
Ihre+Agoda+Buchung+Nr.+77083713+ist+bes %C3%A4tigt%21+Verwalten+Sie+Ihre+B
%E6%82%A8%E7%9A%84Agoda%E8%AE%A2%E5%8D%95%2877083753%29%E5%B7%B2%E7%A%AE%E8%AE%A4%EF%BC%81+%E4%BD%BF%E7%94%A8%E6%88%91%E4%BB%AC%E7%9A%84%E5%85%8D%E8%B4%B9%E5%AE%A2%E6%88%B7%E7%AB%AFhttp%3A%2F%2Fapp-agoda.com%2FGetTheApp%EF%BC%8C%E8%BD%BB%E6%9D%BE
Is there any way to convert all of this content to plain English text in Microsoft Excel?
Regards
There isn't a built-in function to handle this, but it's possible with a custom function, installing a third-party add-in, or using the substitute command:
Using a custom VBA function
Source: http://www.freevbcode.com/ShowCode.asp?ID=1512
Public Function URLDecode(StringToDecode As String) As String
Dim TempAns As String
Dim CurChr As Integer
CurChr = 1
Do Until CurChr - 1 = Len(StringToDecode)
Select Case Mid(StringToDecode, CurChr, 1)
Case "+"
TempAns = TempAns & " "
Case "%"
TempAns = TempAns & Chr(Val("&h" & _
Mid(StringToDecode, CurChr + 1, 2)))
CurChr = CurChr + 2
Case Else
TempAns = TempAns & Mid(StringToDecode, CurChr, 1)
End Select
CurChr = CurChr + 1
Loop
URLDecode = TempAns
End Function
With third-party add-in
Source: SeoTools (needs installation)
=UrlDecode(your_string_here)
With substitute command
Source: https://searchmarketingcorner.wordpress.com/2013/03/27/creating-an-excel-formula-to-encode-or-unencode-urls/
Paste the formula below to the right of your cell in order to URL decode the contents of that cell
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CELL_TO_BE_DECODED,"%3F","?"),"%20"," "),"%25", "%"),"%26","&"),"%3D","="),"%7B","{"),"%7D","}"),"%5B","["),"%5D","]")
Or for working with GUIDs, add one more SUBSTITUTE for the dashes.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CELL_TO_BE_DECODED,"%3F","?"),"%20"," "),"%25", "%"),"%26","&"),"%3D","="),"%7B","{"),"%7D","}"),"%5B","["),"%5D","]"),"%2D","-")
For completeness, here is the reverse formula for URL encode. This is the same as the URL encode formula but positions of new_text and old_text swapped around.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CELL_TO_BE_ENCODED,"?","%3F")," ","%20"),"%","%25"),"&","%26"),"=","%3D"),"{","%7B"),"}","%7D"),"[","%5B"),"]","%5D")
Or for working with GUIDs, add one more SUBSTITUTE for the dashes.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CELL_TO_BE_ENCODED,"?","%3F")," ","%20"),"%","%25"),"&","%26"),"=","%3D"),"{","%7B"),"}","%7D"),"[","%5B"),"]","%5D"),"-","%2D")
Here is a User Defined Function (UDF) that actually works.
In a standard code module, place this routine:
Public Function URLDecode(url$) As String
With CreateObject("ScriptControl")
.Language = "JavaScript"
URLDecode = .Eval("unescape(""" & url & """)")
End With
End Function
Now you can call it from the worksheet, just like a built-in Excel function.
For example, if your encoded URL text were in cell A1, you could enter the following formula in cell B1:
=URLDecode(A1)
That's it. The fully decoded URL is now in cell B1.
Note that this is the real deal. It is not an attempt to replace a couple of characters. This uses the full power of JavaScript by way of the Microsoft Script Control to completely decode the URL.
The following adds decoding for commas in URLs. Just an additional SUBSTITUTE for %2C. This just adds to Carlos's post from 4 years ago.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CELL_TO_BE_DECODED,"%3F","?"),"%20"," "),"%25", "%"),"%26","&"),"%3D","="),"%7B","{"),"%7D","}"),"%5B","["),"%5D","]"),"%2D","-"),"%2C",",")
You can do it without VBA using array formulas. If A1 is the cell to be decoded, enter this formula, and then press Ctrl-Shift-Enter:
=TEXTJOIN("", FALSE,
MID(A1,
FIND("*",
SUBSTITUTE("%DD"&A1,"%","*", ROW(INDIRECT("1:"&LEN(A1)-LEN(SUBSTITUTE(A1,"%",""))+1)) )
),
FIND("*",
SUBSTITUTE(A1&"%","%","*", ROW(INDIRECT("1:"&LEN(A1)-LEN(SUBSTITUTE(A1,"%",""))+1)) )
) - FIND("*",
SUBSTITUTE("%EE"&A1,"%","*", ROW(INDIRECT("1:"&LEN(A1)-LEN(SUBSTITUTE(A1,"%",""))+1)) )
)
) & IFERROR(CHAR(HEX2DEC(MID(A1,
FIND("*",
SUBSTITUTE(A1&"%","%","*", ROW(INDIRECT("1:"&LEN(A1)-LEN(SUBSTITUTE(A1,"%",""))+1)) )
)+1,
2
))),"")
)
It won't work if you don't Ctrl-Shift-Enter.
Related
I set up macro, that fills my PDF form. I need plus sign in front of positive numbers, like +2,75. I'm using this bit of code.
Application.SendKeys Range("D" & CustRow).Value, True
I've tried to use this and many other variants. With no succes.
Application.SendKeys Format(Range("D" & CustRow).Value,""+"#.##;-#.##"), True
Can someone help me, please?
There's at least 3 things wrong in your code:
The symtax ""+"#.##;-#.##" means concatenate "" (an empty-string) with the string "#.##;-#.##" (i.e. the + is a VBA operator, not a SendKeys key).
Assuming that you meant "+#.##;-#.##" instead, then that won't work either, because VBA's SendKeys treats the +, ^, and % symbols as special symbols that represent the Shift, Ctrl, and Alt modifier keys respectively.
Most importantly: there is no "+" key on a keyboard. To enter a "+" character you have to press [Shift] + [=], therefore you need to send "+=" to Acrobat via SendKeys, with the numeric/digit characters to follow.
While not "wrong" in this particular case, your code incorrectly assumes that a VBA Format() string will produce output that you can directly pass to SendKeys - but Format() and SendKeys both look-for and handle their own sets of special-characters and have their own escape sequences. You should Format() the Excel cell value first, then generate the SendKeys string from that string using intermediate variables: this will also help with debugging because right now you can't see what string is being sent to SendKeys (VBA's debugger is not very sophisticated seeming as it's essentially unchanged since Office 2000).
So try this: (I don't have an Acrobat form to test this with, so I don't know for certain if it will work or not)
Dim cellRef As String : cellRef = "D" & CustRow
Dim cellValue As Variant: cellValue = Range( cellRef ).Value
Dim formatted As String : formatted = Format( cellValue, "+#.##;-#.##" )
Dim keys As String : keys = Replace( formatted, "+", "+=" )
Application.SendKeys keys, True
I am wondering is it possible to have custom number format using Excel formula that will not be dependent on localization of Excel application (EU/US)?
For example I have value 1291660.
Then using formula =TEXT(A1;"# ##0,00"). I get as an output 1 291 660,00. The target is to have in any case 1.291.660,00 as an output. Any Excel professional to give an advice?
I have tried =TEXT(A1;"#.##0,00") - This didn't work
I think VBA is the only solution to this. I have found my old question about the same topic, but it seems that solution provided is not working for some reason?
Ultimate 1000 separator using VBA
Function CustomFormat(InputValue As Double) As String
Dim sThousandsSep As String
Dim sDecimalSep As String
Dim sFormat As String
sThousandsSep = Application.International(xlThousandsSeparator)
sDecimalSep = Application.International(xlDecimalSeparator)
' Up to 6 decimal places
sFormat = "#" & sThousandsSep & "###" & sDecimalSep & "######"
CustomFormat = Format(InputValue, sFormat)
If (Right$(CustomFormat, 1) = sDecimalSep) Then
CustomFormat = Left$(CustomFormat, Len(CustomFormat) - 1)
End If
' Replace the thousands separator with a space
' or any other character
CustomFormat = Replace(CustomFormat, sThousandsSep, " ")
End Function
By replacing CustomFormat = Replace(CustomFormat, sThousandsSep, " ") with CustomFormat = Replace(CustomFormat, sThousandsSep, ".") output is .1 291 660
You may use:
=SUBSTITUTE(SUBSTITUTE(FIXED(A1,2,0),",","."),".",",",INT(LEN(A1)/3)+1)
The way it works is that on an EU-system FIXED() will return: 1.291.660,00 but on an US-system it should return 1,291,660.00. To create the same output-string, we can SUBSTITUTE() all comma's to dots. A 2nd SUBSTITUTE() will then replace only the last dot back to a comma. To find the right index I used INT(LEN(A1)/3)+1 which works well on itegers like 1291660. If you happen to have decimal values, you can change this to:
=SUBSTITUTE(SUBSTITUTE(FIXED(A1,2,0),",","."),".",",",INT(LEN(INT(A1))/3)+1)
EDIT:
The above should always return the desired format, but it's a string. To return the numeric value in any further calculations, you can use NUMBERVALUE():
=NUMBERVALUE(C1,",",".")
Go to excel file tab, click options and then the following options as desired
Uncheck use system separators and define your own
You don't need VBA for this. You can use SUBSTITUTE to replace the default separator characters, and you can detect what these are by cutting them out from the formatted string of a known number. I use ASCII 1 (SOH) character to avoid replacing twice (e.g. replacing thousands separator from " " to ".", than replacing decimal separators from "." to "," would cause that thousands separators appear as ","):
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(1234567.89,"# ##0.000"),MID(TEXT("# ##0",1000),2,1),CHAR(1)&" "),MID(TEXT("0.0",0.1),2,1),CHAR(1)&","),CHAR(1)&" ","."),CHAR(1)&",",",")
This will output "1.234.567,890".
This output will appear as a string (you cannot add numbers to it, and it is left adjusted by default), and you cannot change this behavior if you don't use Excels local settings for separators.
BTW, using " " for thousands separator and either "." or "," for decimals is the clearest way of displaying numbers.
I have a text in a worksheet like:
The girl is very beautiful
I want a formula to perform a search from right to left for the word "very", and if found then extract it to some other region of the sheet.
Note: Purpose of doing reverse search is because I want to implement it in my workbook which requires reverse search.
At least, say me how to revert the text like this :
beautiful very is girl The
Then I can do a normal search. I don't know VBA so please give some formula.
VBA Function to reverse the words in text:
Public Function StrReverse(strIn As String, Optional Delimiter As String = " ") As String
'Reverse the words in 'StrIn', split on a "Space" unless 'Delimiter' is specified
Do While InStrRev(strIn, Delimiter) <> 0
StrReverse = StrReverse & Delimiter & Right(strIn, Len(strIn) - InStrRev(strIn, Delimiter))
strIn = Trim(Left(strIn, InStrRev(strIn, Delimiter) - 1))
Loop
StrReverse = Trim(StrReverse & Delimiter & strIn)
If Left(StrReverse, 1) = Delimiter Then StrReverse = Right(StrReverse, Len(StrReverse) - 1)
End Function
For example, if cell A1 contains:
The girl is very beautiful
...then you could enter in another cell:
=StrReverse(A1)
...which would return:
beautiful very is girl The
To add a custom VBA function to a workbook:
Copy the code for the function you want to add to Excel (from above).
In an Excel, workbook, press Alt + F11 to open the VBA Editor (VBE).
Press Alt + I M to insert a new module.
Press Ctrl + V to paste in the code.
Press Alt + F C to return to Excel.
Edit #1:
Added optional delimiter to function above (defaults to a " " space).
Also, FindReverse (below), which allows VBA's (little-known) InStrRev function to be used on worksheets.
Public Function FindReverse(StringCheck As String, StringMatch As String, _
Optional Start As Long = -1) As Long
'Returns the position number of the last occurrence of 'Stringmatch"
'within StringCheck', Optionally specify the position number from the
'end to begin the search. (-1 = Begin at the end)
FindReverse = InStrRev(StringCheck, StringMatch, Start)
End Function
Edit #2:
LOL # Myself ... I'm always telling people not to try to recreate functionality that's already built into MS Office, and it seems that I unwittingly did the same thing -- even giving it the same as the existing VBA Function.
Built-in VBA function:
I realize that it's not identical functionality as the StrReverse function I wrote (above) but I suspect it also could have solved OP's original inquiry...
Nonetheless, I am really surprised that VBA even allows a custom function to have the same name as a built-in function!
How to confuse VBA:
I have stings like this which are addresses, e.g.:
P.O. Box 422, E-commerce park<br>Vredenberg<br><br><br>Curaçao
Adelgatan 21<br>Malmö<br><br>211 22<br>Sweden
Läntinen Pitkäkatu 35 A 15<br>Turku<br><br>20100<br>Finland
I am interested in Country only. Country always comes last after a <br> tag.
Note, that there can be several such tags preceding this last value (e.g. 1st example string).
Is there a good way to do a formula may ve along those lines:
Identify end of string
Loop a character back until one reaches ">" character
Cut everything else (including the ">" encountered)
You don't need RegEx to do this if it's always the last part of the string.
You can get it with String modifiers doing
Sub Test()
Dim str As String, str1 As String, str2 As String
Dim Countries As String
str = "P.O. Box 422, E-commerce park<br>Vredenberg<br><br><br>Curaçao"
str1 = "Adelgatan 21<br>Malmö<br><br>211 22<br>Sweden"
str2 = "La¨ntinen Pitka¨katu 35 A 15<br>Turku<br><br>20100<br>Finland"
Countries = Right(str, Len(str) - InStrRev(str, "<br>") - 3)
Countries = Countries + vbNewLine + Right(str1, Len(str1) - InStrRev(str1, "<br>") - 3)
Countries = Countries + vbNewLine + Right(str2, Len(str2) - InStrRev(str2, "<br>") - 3)
MsgBox Countries
End Sub
Obviously this will need to be updated for how your data set is stored. You can loop through the dataset and use the string modifier on each line
A formula works too. If a string in A1, write in B1:
=TRIM(RIGHT(SUBSTITUTE(A1,"<br>",REPT(" ",100)),100))
Modified using an approach taken from here:
https://exceljet.net/formula/get-last-word
In Microsoft Excel file, I have a text in rows that appears like this:
1. Rc8 {[%emt 0:00:05]} Rxc8 {[%emt 0:00:01]} 2. Rxc8 {[%emt 0:00:01]} Qxc8 {} 3. Qe7# 1-0
I need to remove any text appearing within the flower brackets { and }, including the brackets themselves.
In the above example, there are three instances of such flower brackets. But some rows might have more than that.
I tried =MID(LEFT(A2,FIND("}",A2)-1),FIND("{",A2)+1,LEN(A2))
This outputs to: {[%emt 0:00:05]}. As you see this is the very first instance of text between those flower brackets.
And if we use this to within SUBSTITUTE like this: =SUBSTITUTE(A2,MID(LEFT(A2,FIND("}",A2)),FIND("{",A2),LEN(A2)),"")
I get an output like this:
1. Rc8 Rxc8 {[%emt 0:00:01]} 2. Rxc8 {[%emt 0:00:01]} Qxc8 {} 3. Qe7# 1-0
If you have noticed, only one instance is removed. How do I make it work for all instances? thanks.
Highlight everything
Go to replace
enter {*} in text to replace
leave replace with blank
This should replace all flower brackets and anything in between them
It is not that easy without VBA, but there is still a way.
Either (as suggested by yu_ominae) just use a formula like this and auto-fill it:
=IFERROR(SUBSTITUTE(A2,MID(LEFT(A2,FIND("}",A2)),FIND("{",A2),LEN(A2)),""),A2)
Another way would be iterative calculations (go to options -> formulas -> check the "enable iterative calculations" button)
To do it now in one cell, you need 1 helper-cell (for my example we will use C1) and the use a formula like this in B2 and auto-fill down:
=IF($C$1,A2,IFERROR(SUBSTITUTE(B2,MID(LEFT(B2,FIND("}",B2)),FIND("{",B2),LEN(B2)),""),B2))
Put "1" in C1 and all formulas in B:B will show the values of A:A. Now go to C1 and hit the del-key several times (you will see the "{}"-parts disappearing) till all looks like you want it.
EDIT: To do it via VBA but without regex you can simply put this into a module:
Public Function DELBRC(ByVal str As String) As String
While InStr(str, "{") > 0 And InStr(str, "}") > InStr(str, "{")
str = Left(str, InStr(str, "{") - 1) & Mid(str, InStr(str, "}") + 1)
Wend
DELBRC = Trim(str)
End Function
and then in the worksheet directly use:
=DELBRC(A2)
If you still have any questions, just ask ;)
Try a user defined function. In VBA create a reference to "Microsoft VBScript Regular Expressions 5.5. Then add this code in a module.
Function RemoveTags(ByVal Value As String) As String
Dim rx As New RegExp
rx.Global = True
rx.Pattern = " ?{.*?}"
RemoveTags = Trim(rx.Replace(Value, ""))
End Function
On the worksheet in the cell enter: =RemoveTags(A1) or whatever the address is where you want to remove text.
If you want to test it in VBA:
Sub test()
Dim a As String
a = "Rc8 {[%emt 0:00:05]} Rxc8 {[%emt 0:00:01]}"
Debug.Print RemoveTags(a)
End Sub
Outputs "Rc8 Rxc8"