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
Related
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
This is probably a simple question but I just can't figure it out.
I've built a macro to convert values in Celsius to Fahrenheit in a range. The problem is, sometimes these comes with > or < than, and sometimes with ≥ or ≤ signs. I can not figure out a way to get VBA to recognize the ≥ or ≤ signs at all. I've tried ASCII and unicode and neither seems to be working.
I've included the code here (With my attempts at Unicode in place of the ≥ or ≤ symbols)
As it stands, it just replaces those symbols with nothing at all instead of putting them back into place.
Sub Celsius_to_Fahrenheit()
Set myRange = Selection
For Each myCell In myRange
LastString = ""
GTLT = ""
For i = 1 To Len(myCell.Value)
mt = Mid(myCell.Value, i, 1)
If mt Like "[0-9]" Or mt Like "-" Or mt Like "." Then
tstring = mt
ElseIf mt Like "–" Or mt Like "—" Then
tstring = "-"
ElseIf mt Like ">" Or mt Like "<" Or mt Like ChrW(U + 2264) Or mt Like ChrW(U + 2265) Then
GTLT = mt
tstring = ""
Else
tstring = ""
End If
LastString = LastString & tstring
Next i
myCell.Value = GTLT & 32 + (9 / 5) * LastString & " °F"
Next
Selection.Replace What:="-", Replacement:="–", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="111111111111", Replacement:="1", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="222222222222", Replacement:="2", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="333333333333", Replacement:="3", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="444444444444", Replacement:="4", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="555555555555", Replacement:="5", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="666666666666", Replacement:="6", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="777777777777", Replacement:="7", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="888888888888", Replacement:="8", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="999999999999", Replacement:="9", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="° °", Replacement:=" °", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
I have a set of data which only pulls as MMM-DD-YYYY. I'd like to convert it to a date (MM/DD/YYYY format) to look it up versus another set of data.
I recorded a macro to simply replace the months with their respective numbers individually but I know there has to be a better way to do this. Below is my
current code:
With ws1.Cells
.Replace What:="jan-", Replacement:="01-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="feb-", Replacement:="02-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="mar-", Replacement:="03-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="apr-", Replacement:="04-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="may-", Replacement:="05-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="jun-", Replacement:="06-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="jul-", Replacement:="07-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="aug-", Replacement:="08-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="sep-", Replacement:="09-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="oct-", Replacement:="10-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="nov-", Replacement:="11-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:="dec-", Replacement:="12-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End With
This will convert your text string into a true date for the active cell:
Sub datefix()
Dim s As String
s = ActiveCell.Value
arr = Split(s, "-")
ActiveCell.Value = arr(1) & " " & arr(0) & " " & arr(2)
End Sub
You can format it or loop it to your heart's content.
(I am using US locale)
EDIT#1:
With your desired format:
Sub datefix()
Dim s As String
s = ActiveCell.Value
arr = Split(s, "-")
ActiveCell.Value = arr(1) & " " & arr(0) & " " & arr(2)
ActiveCell.NumberFormat = "mm/dd/yyyy"
End Sub
Before:
and after:
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
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