I am writing a VB Script to convert Alpha Numerics to Special Characters and Vice-Versa. The script worked fine when I tried to convert Alpha Numeric values to Special Characters but it just returns 'X' when I try to convert Special Characters to Alpha Numeric. I am not sure why. I have pasted the code below. Any help is appreciated.
fnd = Array("~","\",">","!","#","#")
rplc = Array("A", "B", "C","1","2","3")
For x = LBound(fnd) To UBound(fnd)
'Loop through each worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=fnd(x), Replacement:=rplc(x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=True, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Next x
There were certain characters that needed an escape character before them. So I replaced
"~", "*", "?"
With
"~~", "~*", "~?"
And it worked. Thanks for all the help.
Related
I have a column range (stored as text) where I have to replace second last digit from 7 to 5 for only cells ending in "-fsa". Here is an example
Find
52881871-fsa
Replace
52881851-fsa
I tried using wildcard, however it only works in find function but doesn't work in replace function.
Thank You for the help!
I used following in my code, the find does what's asked but replace doesn't
Selection.Replace What:=("??????7?-fsa"), Replacement:=("??????5?-fsa"), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
You can use wildcards only in the find pattern but not in the replace pattern. You need to use Selection.Find instead of .Replace. And then split the found string to replace the 7 with a 5.
Dim FoundAt As Range
Set FoundAt = Selection.Find(What:=("??????7?-fsa"), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False)
If Not FoundAt Is Nothing Then
FoundAt.Value = Left$(FoundAt.Value, 6) & "5" & Right$(FoundAt.Value, 5)
End If
Alternatively you can do that with regular expressions: How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
I am looping through cells trying to make certain parts of cell values bold. I have a cell with contents:
<b>This part should be bold</b> but this should not be
I can get the correct part to be bold but the next step is to remove the tags. The following lines cause an issue:
Cells.Replace What:="<b>", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="</b>", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
When these lines run (in either order), the whole cell value becomes bold. More specifically, after running it line by line, running either line will have the same result. I am new to VBA and not sure what's causing this.
The function I'm using to make the substring bold is:
fCell.Characters(Start:=m, Length:=n - m + 1).Font.Bold = True
where fCell is being looped over and m and n are the indices locating <b> and </b> respectively.
Once you've set a format for only part of a cell's content, you cannot replace the whole content without losing that partial formatting.
You need to use the Characters method to remove the tags: use Instr() to find the location then set the text there to "":
Sub tester()
RemoveStrings Range("A1"), Array("<b>", "</b>")
End Sub
Sub RemoveStrings(c As Range, Strings)
Dim txt, pos As Long
For Each txt In Strings
Do
pos = InStr(1, c.Value, txt, vbTextCompare)
If pos > 0 Then c.Characters(pos, Len(txt)).Text = ""
Loop While pos > 0
Next txt
End Sub
I am trying to do a find and replace in excel but excel is not finding anything and i think it is due to the amount of characters as there are some that have around 30,000 and for example find Don't and replace with Dont.
I want to insert this data into SQL and this is why i am trying to remove all the single quotes, there are many Q and A's on find and replace but i cant find anything that works for the amount of characters my data has.
I am terrible at VBA and so i don't really have any code to share except the below which doesn't work.
Selection.Replace What:="'", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Thanks to #Naresh who answered this.
Sub FindString()
Dim c As Range
Dim firstAddress As String
With Worksheets(1).Range("A1:a10")
Set c = .Find("'", LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = Replace(c.Value, "'", "quotechanged")
Set c = .FindNext(c)
Loop While Not c Is Nothing
End If
End With
End Sub
I am not sure this is a question for here, so shoot me if it's not!
I have a spreadsheet that contains data of both number and currency format. I am to delete the values in the cells that are of currency format only.
I can do this with a simple For Each loop, however, due to the size of the spreadsheet this is not efficient.
Option Explicit
Sub ClearCurrency()
Dim myRange As Range
Dim cell As Range
Set myRange = Selection
For Each cell In myRange
If cell.NumberFormat = "$#,##0.00" Then
cell.ClearContents
End If
Next cell
End Sub
I have read up on VarTypes and .NumberFormat but am unable to piece together a more efficient solution.
I know I cannot store the information to an array to loop through, so is there a faster way?
As proposed in comments, you could use SearchFormat and ReplaceFormat to do this, something like the following:
Sub ClearCurrency()
If Not TypeOf Selection Is Excel.Range Then Exit Sub
With Application
.FindFormat.Clear
If .DecimalSeparator = "." Then .FindFormat.NumberFormat = "$#,##0.00"
If .DecimalSeparator = "," Then .FindFormat.NumberFormat = "\$#,##0.00"
End With
Selection.Replace What:="*", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=False
End Sub
Noteworthy
Whilst working on non en_US local settings and making use of custom numberformat "$#,##0.00" you may encounter an Error 1004 warning. The format is not recognized and will error out. To prevent this you can either; make use of NumberFormatLocal:
Application.FindFormat.NumberFormatLocal = "$#.##0,00"
Notice the difference in both decimal comma and grouping character. A second option would be to escape the sequence with a backslash that indicates that the following character should be used literally as is:
Application.FindFormat.NumberFormat = "\$#,##0.00"
Now the decimal point and grouping character can stay in place. Therefore we need a check to test the local settings, which can be done with Application.DecimalSeparator, as proposed in the current solution.
This will do:
Set myRange = Selection
Application.FindFormat.NumberFormat = "$#,##0.00"
myRange.Replace What:="*", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=False
I have a spreadsheet that contains emojis, e.g., 😃, and I am looking for a solution to use Excel VBA to replace the emojis with null.
Emojis can be removed using the Excel replace action, so I recorded a macro to automate the replace. I opened the recorded macro and it was displayed as follows:
Sub Remove_Emojis()
Cells.Replace What:="??", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
End Sub
The problem is that VBA doesn't recognize emojis (😃) and replaces them with "??", i.e., Unicode characters above a certain value are not recognized by VBA.
I tried replacing "??" with ChrW():
Sub Remove_Emojis()
Cells.Replace What:=ChrW(128515), Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
End Sub
but this results in an error:
Invalid procedure call of argument
because the ChrW() function does not allow a value above 65535. Note: The ChrW() function works if the value of the argument is within the range -32,767 to 65,535.
I expect that there should be support for doing this in VBA given that it can be done in Excel.
I did as small experiment, putting your smiley into excel and let the following code run:
Dim s
s = ActiveCell
Dim i As Long
For i = 1 To Len(s)
Dim c
c = Mid(s, i, 1)
Debug.Print i, c, AscW(c)
Next i
My result was
1 ? -10179
2 ? -8701
So obviously, the single character is split into 2 inside VBA. AscW and it's pendant ChrW deal with 16bit, and the emoji is a 32bit char, so in VBA this emoji character is handled as if there are 2 characters in the string
I added the following code and voilà , the smiley char was gone:
Dim x
x = ChrW(-10179) & ChrW(-8701)
s = Replace(s, x, "(smiley)")
ActiveCell.Offset(0, 1) = s
Probably you have to experiment with the different emojis you are facing and build a list in your replacing routine.
Thank you to FunThomas for pointing out that emojis are represented as 2 characters in VBA. The revised VBA code that works based on this:
Sub Remove_Emojis()
Cells.Replace What:=ChrW(-10197) & ChrW(-8701), Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
End Sub
In my actual solution, I put this into a loop to remove all of the different emojis.