VBA replace text without replacing other words that contain similar text - vba

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

Related

How to use the find funtion to search for the Value of a text box?

I am trying to make a userform that can bring up data using an ID number.
I am trying to reference a text box and select it, and then using it as a reference to fill out the Time and comments in the sheet. I think the is I cant put "txtID.Value" into the Find function.
Here is an example of my code:
Sheet1.Select
Columns("A:A").Select
Selection.Find(What:="txtID.Value", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
ActiveCell.Offset(0, 8).Value = txtTime2
ActiveCell.Offset(0, 9).Value = txtComment2
When using the Find function, it's recommended to use a Range object, and set it to the result. This method allows you to trap a possible scenario where Find failed to find a match in the searched range Sheet1.Columns("A:A").
Also, try to avoid using Select, Selection and ActiveCell, and use fully qualified Range objects (like in the code below).
Code
Dim FndRng As Range
Set FndRng = Sheet1.Columns("A:A").Find(What:=txtID.Value, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not FndRng Is Nothing Then ' successful find
FndRng.Offset(, 8).Value = txtTime2
FndRng.Offset(, 9).Value = txtComment2
Else ' unable to fins the value in txtID
MsgBox "Unable to find " & txtID.Value & " in Sheet1"
End If
Note: if you have this code outisde the User_Form module, then you need to add the User_Form reference when trying to get the txtID.Value.
For eaxmple, let's say the name of your form is UserForm1, then change this line to:
Set FndRng = Sheet1.Columns("A:A").Find(What:=UserForm1.txtID.Value, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
To Make it work, you will have to put the code as below, which is quite self explainatory.
Selection.Find(What:=Userform1.textbox1.value, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
hope this helps

VBA Copy only non blank cells

does anyone one how to instead of static range:
'~~> Copy the range
wsI.Range("A1:K50").Copy
To change it to dynamic where A1:A50 is calculated depending on non-blank cells.
So if Sheet1 has 23 lines and the rest of them are blank so range would be: A1:K23?
just in case someone looks for an answer:
issue - when copying data from other sheet where I had formulas, pasted cells are blank, but not actually blank. It holds some sort of 'nothing'...
solution ,SpecialCells(xlCellTypeVisible).Copy to copy data and paste in the same workbook with Paste:=xlPasteValuesAndNumberFormats (it holds date value as this was something I needed). Then used "find and replace" code to clear "blank, but not blank" cells.
'~~> Find "" and replace with pneumonoultramicroscopicsilicovolcanoconiosis
Worksheets("paste").Range("A1:K500").Cells.Replace What:="", Replacement:="pneumonoultramicroscopicsilicovolcanoconiosis", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'~~> Find pneumonoultramicroscopicsilicovolcanoconiosis and replace with ""
Worksheets("paste").Range("A1:K500").Cells.Replace What:="pneumonoultramicroscopicsilicovolcanoconiosis", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

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 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

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.