Replace special characters with VBA - 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

Related

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

VBA find, how to capture when there is nothing to find

I just wrote a seven page macro with two UserForms that is a thing of beauty.
There are two data bases and the one on the left is longer than the one on the right.
I use a variable DDataa1 and the VBA function:
Sub SSearchh()
... code ...
Cells.Find(What:=DDataa1, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
... code ...
End Sub
It works perfectly EXCEPT when there is no DDataa1 to find. (That is, there is no variable in the right hand list that is being searched for in the left hand list.)
In that case the Macro just stops. I want to capture this "data not found" event and write more code specific to this failure but this function does not seem to generate a True/False condition.
Specific assistance would be appreciated
You could try
Dim FindRange As Range
Set FindRange = Cells.Find(What:=DDataa1, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If FindRange Is Nothing Then
' do error handling here
Else
FindRange.Activate
End If

VBA: How do I search for a formula driven value?

I am trying to search for the date, which I have in cell K1 as =today()....
Whenever I am recording the macro, however, it will continue to use the date which the macro was written. What I am curious about is a methodology of pasting what is on the clipboard into the VBA search function.
So I want to search column A for what is in cell K1. This is what code I have now (the find what is what I just typed to help you all have an idea of what I'm looking for)
Range("K1").Select
Selection.Copy
Columns("A:A").Select
Selection.Find(What:="COPY CONTENTS OF CELL K1", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
This should do it. Note that your original code will throw an error if today's date isn't found. Typically when working with Find() it's safer to test the returned value before trying to do something with it.
Dim f As Range
Set f = Columns("A:A").Find(What:=Date(), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing then
'do something with f
Else
'???? do what
End If
You could replace your entire code with this:
Activesheet.Columns("A:A").Find(What:=ActiveSheet.Range("K1"), After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
What was happening is that there's no way to paste a cell's contents via .Copy directly into a VBA function. So I placed what you wanted to paste into it into the function directly.
I think you will also find this helpful:
How to avoid using Select in Excel VBA macros

How to handle wild characters in Macros?

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.