How to handle wild characters in Macros? - vba

I have wrote a piece of code to replace any characters, but it doesn't work fine with *. Can some one guide how I can handle *.
Here is the code:
nextrow = ActiveCell.Row
tocolnbr = ActiveCell.Column
Columns(tocolnbr).Select
Selection.Replace What:="'", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Use a ~ tilde as per Office Support.
Microsoft Excel uses the tilde (~) as a marker to indicate that the next character is a literal. When you use the Find and Replace dialog box to find or replace a character such as a tilde (~), an asterisk (*), or a question mark (?), you must add a tilde (~) before the character in the Find what box.
So it must be something like this:
Selection.Replace What:="~*", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Use it's Chr value
Selection.Replace What:=Chr(42), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
EDIT:
I posted without checking. Your code works to replace "*" with nothing, although what you posted is replacing "'" with nothing.

I just used this,
Sub Button1_Click()
Dim rng As Range
Set rng = Range("K2")
rng = Replace(rng, "*", "")
End Sub
And it removed the *
Is it a Character? Or just an asterix?
For an entire range of cells,
Sub Button1_Click()
Dim rng As Range, c As Range
Set rng = Range("K2:K20")
For Each c In rng.Cells
c = Replace(c, "*", "")
Next c
End Sub
I have seen on some occasions where you would have to use the tilde "`" symbol but I don't know if is required here.

Related

Replacing Column Header Text using Macro in Excel

I'm trying to replace the headers in Columns B and C with a different header text with the following code:
Columns("B").Replace What:="PN-ASSIGN", _
Replacement:="SERV BAL.", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("C").Replace What:="MISC COST", _
Replacement:="EQUIP BAL.", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
But when I try to use it, the code doesn't seem to do anything. I'm not sure what I'm missing.
Any help would be appreciated!
You need to qualify your sheet as it sounds like your macro is relying on the Active Sheet which is bad practice. You can use a With statement if you are referring to you ws multiple times. Also, you do not need to indicate all the options for the .Replace method. If you do not specify, they default to false. The below code should be sufficient as is.
Also, Option Explicit for good measure
Option Explicit
Sub Headers()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Range("B:B").Replace "PN-Assign", "Serv Bal.", xlPart
ws.Range("C:C").Replace "MISC COST", "EQUIP BAL.", xlPart
End Sub

Replacing dots and whitespace

Columns("P:P").Select
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:=".", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
I am using this code to replace dots and whitespaces. My problem is that The whitespace removal is not working in all cases. If for example i have string "hello " it should replace the whitespace at the end of hello but doesn't. This string is imported from another worksheet. I delete whatever that space is at the end of hello and manually replace with a whitespace, then the vba code works to replace that whitespace. It is almost like whatever that trailing character is, it is not a whitespace. Then with dots so for example "hello......" it will remove only two dots so I get "hello....". Anyone have any suggestions why I am facing this issue?
To removing the whitespace, I suggest to use the TRIM function of VBA and code as following:
Sub DeWhitespace()
Dim a As Long
a = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Range("Q2", "Q" & a).FormulaR1C1 = "=TRIM(RC[-1])"
End Sub

VBA replace text without replacing other words that contain similar text

I'm trying to use "*" as a wildcard for data in Column F. All the data is in the same format and length as "Pie**" where each "*" represents (0-9)
Example: Pie22, Pie71, Pie15, Hot22, Hot41, Hot98
When I try the code below, I run into the issue where CoffeeXX is finding and replacing "Hot" in "hotdog" and it becomes "chickendog".
Is there a was to fix this so that the .find sees the wildcard instead of just picking anything that contains the text?
Set PieXX = Columns("F").Find(What:="Pie**", Lookin:=xlvalues)
If not PieXX is nothing then
Columns("F").Replace What:"Pie**", replacement:="Hotdog", _
SearchOrder:=xlbyrows, Matchcase:=False, SearchFormat:=False, Replaceformat:=false
End If
Set CoffeeXX = Columns("F").Find(What:="Hot**", Lookin:=xlvalues)
If not CoffeeXX is nothing then
Columns("F").Replace What:"Hot**", replacement:="Chicken", _
SearchOrder:=xlbyrows, Matchcase:=False, SearchFormat:=False, Replaceformat:=false
End If
For each cell in Range("F1", Range("F1").End(xlDown))
If cell.value like "pie??" then cell.Replace "pie", "hi", LookAt:=xlPart
Next
This is untested

Replace special characters with VBA

I'm trying to replace romanian characters (like "șțȘȚ") from an Excel file using VBA, but I can't figure it out.
In the VBA editor, if I try to type "ș" or "ț" it is replaced by "?". Why???
I even tryed a different approach like:
Selection.Replace What:="ş", Replacement:="s", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
but it does absolutelly nothing...
Please help.
I've just tried a quick solution for ș which has the ascii code 351. So it could be described with
ChrW(351)
So here is my code:
Sub replance()
Dim rng As Range
Set rng = Worksheets(1).Columns("A:G")
rng.Replace What:=ChrW(351), Replacement:="s", SearchOrder:=xlByColumns, MatchCase:=True
End Sub

how to replace special character using excel vba

Am having this "�" special character in my excel work book, i want to replace all with ".", when i place this � character in the code , it turns to "?" after i run the macro it replaces everything to "."
Sub FindReplaceAll()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Replace What:="�", Replacement:=".", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next ws
End Sub
Thanks in advance
Try to copy this character in a cell, then have the ASCII number and replace the character by the ASCII num:
Dim num =123
Ex:
Cells.Replace What:=Chr(Num), Replacement:=".", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False