Skip replacement formatting if AY2 is a specific value? - excel

I am adding new categories to a report and need to change my VBA code to incorporate those. I currently have specific values change in specific columns, but will need to add a way to skip over these depending on what information is in the data pulled. Currently, my coding is below and changes all of these values no matter what the category selected is. I need to add something that says IF cell value in AY2 is "Total Fresh Meat" skip all of this formatting and move to the next step. Any ideas?
Columns("Ay:Ay").Select
Selection.Replace What:="FRESH PORK", Replacement:= _
"MARINATED/SEASONED PORK", LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
FormulaVersion:=xlReplaceFormula2
Columns("AW:AW").Select
Selection.Replace What:="TOTAL FRESH MEAT", Replacement:= _
"MARINATED/SEASONED PORK", LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
FormulaVersion:=xlReplaceFormula2
Sheets("2. Geography Pull").Select
Columns("G:G").Select
Selection.Replace What:="FRESH PORK", Replacement:= _
"MARINATED/SEASONED PORK", LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
FormulaVersion:=xlReplaceFormula2
Sheets("1. Weekly RMA").Select
Columns("h:h").Select
Selection.Replace What:="FRESH PORK", Replacement:= _
"MARINATED/SEASONED PORK", LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
FormulaVersion:=xlReplaceFormula2
Sheets("4. Reports 1, 4 and 6").Select
Columns("am:am").Select
Selection.Replace What:="FRESH PORK", Replacement:= _
"MARINATED/SEASONED PORK", LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
FormulaVersion:=xlReplaceFormula2

Related

Why doesn't my VBA code work when the selection starts with a dot?

I've created this code that is supposed to replace the commas in a selection with dots if there are any. The code, however, doesn't work if the selection starts a cell containing a dot, but it works if it starts with a cell containing a comma. It even works if it starts with a comma, then a cell with a dot and then again a cell with a comma. Here is the code:
Public Sub DeleteDotsReplaceCommasWithDots()
For Each cell In Selection
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
If InStr(ActiveCell.Value, ".") > 0 And InStr(ActiveCell.Value, ",") > 0 Then
Selection.Replace What:=".", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
ElseIf InStr(ActiveCell.Value, ".") = 0 And InStr(ActiveCell.Value, ",") > 0 Then
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormul
End If
Next cell
End Sub
Any idea about why this is happening? Thanks :)
You seem to be running your search/replace on ActiveCell, rather than each cell you're cycling through with your For..Each loop. I've tidied it up slightly using the With cell.. End With block.
Try this:
Public Sub DeleteDotsReplaceCommasWithDots()
For Each cell In Selection
With cell
.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
If InStr(.Value, ".") > 0 And InStr(.Value, ",") > 0 Then
.Replace What:=".", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
ElseIf InStr(.Value, ".") = 0 And InStr(.Value, ",") > 0 Then
.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula
End If
End With
Next cell
End Sub

Finding and replacing one column in current sheet on Mac

I found the below code, it works good when "search in sheet".
But sometimes, when I open the excel, it default shows "search worksheet". Please see the image.
Then the code will replace entire sheets instead of Columns("V").
Any additional code can control this thing happens?
Thank you in advance.
Sub ReplaceTitleMs()
Columns("V").Replace What:="Ms ", _
Replacement:="", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End Sub
So here is my currently code:
Sub Test_Replace()
Worksheets("Sheet4").Columns("B").Replace What:="XXX", _
Replacement:="KKK", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Worksheets("Sheet4").Columns("B").Replace What:="SIN", _
Replacement:="OOO", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End Sub
When select "search in worksheet" in the menu, and run the code.

VBA code to find and replace using maybe wildcard?

How can I find and remove X from strings, in other words, replace NX1 with N1, NX2 with N2, NX7535 with N7535, all strings start with N but not all have X after N, if they do I need to remove that X, Below I put crazy code I adapted from excel recording but it has to be easier way to do it:
Sub Find_NX_Replace()
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Replace What:="NX1", Replacement:="N1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NX2", Replacement:="N2", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NX3", Replacement:="N3", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NX4", Replacement:="N4", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NX5", Replacement:="N5", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NX6", Replacement:="N6", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NX7", Replacement:="N7", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NX8", Replacement:="N8", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NX9", Replacement:="N9", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Example:
Do a loop of the number:
Sub Find_NX_Replace()
Dim i as Long
For i = 1 To 9
Selection.Replace What:="NX" & i, Replacement:="N" & i, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next i
End Sub

VBA Remove parentheses from columns

I'm trying to format a spreadsheet by searching through three columns and removing any parentheses. Currently, I have:
Range("B:D").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
When I run the code, I get:
Run-time error '1004':
Application-defined or object-defined error"
Does it work if you qualify with Sheet Name, not using Select?
Sheet1.Range("B:D").Replace What:="(", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Sheet1.Range("B:D").Replace What:=")", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

How can I make an automated macro on excel?

I have made this macro:
Sub clean()
' clean Macro
ChDir "C:\_deletelater\xls"
Workbooks.OpenText filename:="C:\_deletelater\xls\traxreport.xls", Origin:= _
437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
Range("A1:AD18").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.Replace What:="DYN", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="WOO", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="MIS", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="BAS", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="BAR", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="DLC", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="SYN", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveWorkbook.SaveAs filename:="C:\_deletelater\xls\traxreport.csv", _
FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
I want to just click on my xlsm file and when it opens, I would want the file that gets saved in:
ActiveWorkbook.SaveAs filename:="C:\_deletelater\xls\traxreport.csv", _
FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Save
to get created without me having to press run macro.
You will need to make sure the file is set to trusted but you can put code into the ThisWorkbook section behind the scenes that calls the macro when it gets triggered by the workbook being opened.
There's a Microsoft article detailing how to do it... http://office.microsoft.com/en-gb/excel-help/running-a-macro-when-excel-starts-HA001034628.aspx

Resources