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
Related
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
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
I'm trying to use SendKeys to copy a word from one cell into the replace (CTRL+F) function.
The copy bit is fine but this spreadsheet is going to be used as a template so the variable is what is in that cell meaning i'm using send keys.
I'm open to other ideas.
Code is below.
Range("E5:G5").Select
Selection.Copy
Sheets("New Project Schedule").Select
Selection.Replace What:="New Merchant", Replacement:= SendKeys "^v" , LookAt= _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Sheets("Out of Scope").Select
Selection.Replace What:="New Merchant", Replacement:= SendKeys "^v", LookAt= _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Sheets("New Project Schedule").Select
Selection.Replace What:="Merchant", Replacement:= SendKeys "^v", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Sheets("Out of Scope").Select
Selection.Replace What:="Merchant", Replacement:= SendKeys "^v", LookAt:=
xlPart , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
You don't need to copy and paste - something like this should do:
Dim avSheets, vSheet
Dim sWord As String
avSheets = Array("New Project Schedule", "Out of Scope")
sWord = Range("E5:G5").Value
For Each vSheet In avSheets
With Sheets(vSheet).Cells
.Replace What:="New Merchant", Replacement:=sWord, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
.Replace What:="Merchant", Replacement:=sWord, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With
Next vSheet
Rather than copying the block of three cells and then trying to determine where to paste them is different worksheets, get the three cells into an array and then decide where the array should be deposited in each of the other sheets.
I have an issue where I need to replace a number with a Loan identifier, the problem I am having is the Loan identifier contains the number that needs replacing so I am ending up with something like "LOAN1592LOAN3161LOAN29269704932LOAN2926970411" when what I actually want is if the loan number is 6 for it to be replaced with LOAN31617932 not the above. Any thoughts how I can stop this from happening? I hope this makes sense. Please see the segment of my code as follows:
Columns("e:e").Select
Selection.Replace What:="5", Replacement:="LOAN15926711", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("e:e").Select
Selection.Replace What:="6", Replacement:="LOAN31617932", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("e:e").Select
Selection.Replace What:="7", Replacement:="LOAN29269704", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("e:e").Select
Selection.Replace What:="8", Replacement:="LOAN57538987", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Use xlWhole:
Sub luxation()
Columns("e:e").Replace What:="5", Replacement:="LOAN15926711", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Before:
and after:
Note:
The 55 and 555 are ignored!
I'd do If (or Select Case) instead. See my comment above as to why. Does this work:
Sub t()
Dim cel As Range, rng As Range
Dim lastRow&
lastRow = Cells(Rows.Count, 5).End(xlUp).row
Set rng = Range("E1:E" & lastRow)
For Each cel In rng
If InStr(1, cel.Value, "2") Then cel.Value = "LOAN15926711"
If InStr(1, cel.Value, "6") Then cel.Value = "LOAN31617932"
' etc. etc.
Next cel
End Sub
If the cell will only have 5, 6, etc. then just do
If cel.Value = "6" Then ...
Edit2: But I'd listen to #GarysStudent, adding the simple xlWhole is what you want. Simple, clean, and easy.
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.