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
Related
I have the Following code following recording a macro:
Sub RemoveLetters()
Cells.Replace What:="C ", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False, _
FormulaVersion:=xlReplaceFormula2
End Sub
Which works fine to remove a single character from an active worksheet. The problem I have is that C is just one of a handful of common individual characters I wish to remove How can I modify this or improve upon this to remove a list of given characters instead of just having to copy this for each?
Update:
Some of the data also contains units such as °C. The macro above unfortunately the above code recognises these characters as independent and therefore returns ° only. Does anyone know how I can get around this?
Add some parameters to your Sub and allow passing in an array of text items to be replaced:
Sub Tester()
RemoveLetters ActiveSheet, Array("C ","D ","E ")
End Sub
Sub RemoveLetters(ws As Worksheet, arrTxt)
dim i As Long
for i=lbound(arrTxt) to ubound(arrTxt)
ws.Cells.Replace What:=arrTxt(i), Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=True, SearchFormat:=False
next i
End Sub
I have a problem with the replace funktion in VBA
In one columns from my table there some Values like 10,5m or 15,354m
I want to replace that with an simple vba command
like this
Columns("E:E").Select
Selection.Replace What:="m", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True
but then my Values are 105 or 15354
When I use the build in command in excel without VBA it works like it should.
How Can I fix it?
Here is a test file with an Import txt file, where I collect the data
https://www.dropbox.com/sh/wtn83rs83dx455s/AABH7MmHKQxVJA6Tx7KVwM54a?dl=0
Thanks for your help
The values are not actually 10,5m or 15,354m but 10,5 and 15,354
it's a formatting thing that shows the "m" even if its not part of the value.
the reason is that your import macro sets the range format:
Range("E:E").NumberFormat = "#0.0""m"""
change it to:
Range("E:E").NumberFormat = "#0.0"
..and the "m" is gone.
I found the problem, at first I must replace "," with a "." after that I can replace "m" with "".
And after that I can set the correct format "#0.0""m"""
Columns("E:E").Select
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True
Selection.Replace What:="m", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True
Range("E:E").NumberFormat = "#0.0""m"""
bye
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.
I am trying to replace certain cells with a vlookup function.
Columns("R").Select
Columns("R").Replace What:="N/A", _
Replacement:="=VLOOKUP(RC[-15], [test.xls]test_data'!$C:$R , 16, FALSE)", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
The current code above does not replace the N/A with the vlookup. It just leaves the cell unchanged.
If I removed the "=" in brackets, so it looks like this:
Replacement:="VLOOKUP(RC[-15], [test.xls]test_data'!$C:$R , 16, FALSE)"
This does replace my cells, but wont be in a formula format.
Can anyone help?
Thanks,
You have several problems with your formula which is preventing Excel from actually accepting it. You are trying to use
=VLOOKUP(RC[-15], [test.xls]test_data'!$C:$R , 16, FALSE)
but that
has a syntax error, due to the missing quotation mark, i.e. [test.xls]test_data' should be '[test.xls]test_data', and
is mixing R1C1 notation (RC[-15]) and A1 notation ($C:$R)
The formula you intended to use was probably
=VLOOKUP(RC[-15], '[test.xls]test_data'!C3:C18 , 16, FALSE)
So you probably meant your code to say
Columns("R").Select
Columns("R").Replace What:="N/A", _
Replacement:="=VLOOKUP(RC[-15], '[test.xls]test_data'!C3:C18, 16, FALSE)", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Now, if your Excel settings are set so that you use R1C1 notation as the default, the correction to your formula is probably all that you will have to change.
But, if you use A1 notation as the default, that formula won't be interpreted correctly due to C3:C18 being ambiguous as to the notation being used.
So you will possibly need to temporarily set Excel to use R1C1 notation:
Application.ReferenceStyle = xlR1C1
Columns("R").Select
Columns("R").Replace What:="N/A", _
Replacement:="=VLOOKUP(RC[-15], '[test.xls]test_data'!C3:C18, 16, FALSE)", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Application.ReferenceStyle = xlA1
I am trying to locate the top 10 in a list of thousands of entries, to create an ongoing report of failures of hardware. The report displays a top 10 for various things. For example top 10 errors, top 10 downtime etc. It is used to target engineers to the most critical machines in a manufacturing facility.
Our macro has worked OK for when we have had 255 errors. We have now extended our error list to 2048, and we envisage that our macros will run incredibly slowly.
I have an idea to utilise the Search and Replace function, and utilise the number of replacements to achieve the top 10. Manually a message box is displayed giving this number. When recording this macro, no message box is seen (which is good) but I cannot locate where the number is.
The function returns a Boolean.
Macro recorded looks like this.
Sub searchmacrotest()
' searchmacrotest Macro
Cells.Replace What:="a", Replacement:="AZ", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="AZ", Replacement:="a", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
The first line simply changes the a to Az, and the second changes it back to a. They run quickly and gave a number of 75 with my list.
Logic:
Use a unique word to replace. While choosing ensure that there is no possibility of the word to occur.
Use Countif to count the occurrences of this unique word after replacing
Code: Try this
Sub GetReplaceCount()
Dim ws As Worksheet
'~~> Set this to a word which is unique
Dim magicword As String: magicword = "Sid" & Format(Now, ddmmyyhhmmss)
'~~> This is what you want to replace
Dim searchText As String: searchText = "a"
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Cells.Replace What:=searchText, Replacement:=magicword, LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'~~> This will give you the number of occurences
Debug.Print Application.WorksheetFunction.CountIf(.UsedRange, "*" & magicword & "*")
.Cells.Replace What:=magicword, Replacement:=searchText, LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With
End Sub