How can I make an automated macro on excel? - 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

Related

Skip replacement formatting if AY2 is a specific value?

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

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

Looping through Word documents to extract table data and place into Excel

I currently need to extract data from a Word table and place it into Excel. I am able to do this on a file by file basis. I need to be able to loop through all the word documents in a file path.
More specifically, I need to be able to open up a word file read the info from the tables on that word file import the information needed below, close that word file and then repeat for all word files (doc, or docx) in a specified folder.
Currently my code is this:
Sub ImportWordTable()
Dim eRow As Long
Dim ele As Object
Dim mainBook As Workbook
Set mainBook = ActiveWorkbook
mainBook.Sheets("Sheet1").Range("A:BB").Clear
Set sht = Sheets("sheet1")
Application.Goto (ActiveWorkbook.Sheets("Sheet1").Range("A1"))
Dim wordDoc As Object
Dim wdFileName As Variant
Dim noTble As Integer
Dim rowNb As Long
Dim colNb As Integer
Sheet1.Range("A1").Select
Dim x As Long, y As Long
x = 1: y = 1
Dim sPath As String
Dim sFil As String
Dim owb As Workbook
Dim twb As Workbook
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")
If wdFileName = False Then Exit Sub
Set wordDoc = GetObject(wdFileName)
With wordDoc
noTble = wordDoc.tables.Count
If noTble = 0 Then
MsgBox "No Tables in this document", vbExclamation, "No Tables to Import"
Exit Sub
End If
For k = 1 To noTble
With .tables(k)
For rowNb = 1 To .Rows.Count
For colNb = 1 To .Columns.Count
Cells(x, y) = WorksheetFunction.Clean(.cell(rowNb, colNb).Range.Text)
y = 0
Next colNb
y = 1
Next rowNb
End With
x = x + 1
Next
Range("A1").Select
ActiveCell.Replace What:="Cotnact InformationName", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
ActiveCell.Replace What:="Email", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
ActiveCell.Replace What:="Contact InformationName", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
ActiveCell.Replace What:="Address", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="Location", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
ActiveCell.Replace What:="Phone", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="Cell", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="Fax", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="Re:", Replacement:=":", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A3").Select
ActiveCell.Replace What:="Preferred Position and RoutePreferred Position(s)" _
, Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
ActiveCell.Replace What:="preferred Route(s)", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A4").Select
ActiveCell.Replace What:="Experience ad skillsDriving experience", _
Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
ActiveCell.Replace What:="Experience and skillsDriving experience", _
Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
ActiveCell.Replace What:="trucks driven", Replacement:="", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="other skills/experience", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
ActiveCell.Replace What:="licensingdriver License", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
Range("A5").Select
ActiveCell.Replace What:="licensingdriver License", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
ActiveCell.Replace What:="license number", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="state/prov.", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="hazmat", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A6").Select
ActiveCell.Replace What:="driving recordlicense ever suspended?", _
Replacement:=":", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
ActiveCell.Replace What:="DUI's", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="DUis", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Replace What:="moving violations in last 3 years", Replacement:= _
"", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
ActiveCell.Replace What:="preventable accidents in last 3 years", _
Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
False, SearchFormat:=False, ReplaceFormat:=False
ActiveCell.Replace What:="employment status", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A7").Select
ActiveCell.Replace What:="employment status", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A8").Select
ActiveCell.Replace What:="job history", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A2").Select
ActiveCell.Replace What:="profile summary", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A9").Select
ActiveCell.Replace What:="Resume", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1:A6").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:=":", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1)), TrailingMinusNumbers:=True
Range("B9").Select
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("B1:I1"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
Dim BlankRow As Long
BlankRow = Range("A65000").End(xlUp).Row + 1
Cells(BlankRow, 1).Select
ActiveSheet.Paste
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("A2"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
BlankRow = Range("A65000").End(xlUp).Cells(1, 9).Select
ActiveSheet.Paste
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("B3:C3"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
BlankRow = Range("A65000").End(xlUp).Cells(1, 10).Select
ActiveSheet.Paste
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("B4:D4"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
BlankRow = Range("A65000").End(xlUp).Cells(1, 12).Select
ActiveSheet.Paste
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("B5:F5"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
BlankRow = Range("A65000").End(xlUp).Cells(1, 15).Select
ActiveSheet.Paste
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("B6:E6"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
BlankRow = Range("A65000").End(xlUp).Cells(1, 20).Select
ActiveSheet.Paste
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("A7"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
BlankRow = Range("A65000").End(xlUp).Cells(1, 24).Select
ActiveSheet.Paste
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("A8"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
BlankRow = Range("A65000").End(xlUp).Cells(1, 25).Select
ActiveSheet.Paste
Application.Goto (ActiveWorkbook.Sheets("sheet1").Range("A9"))
Selection.Copy
Application.Goto (ActiveWorkbook.Sheets("sheet3").Range("A2"))
BlankRow = Range("A65000").End(xlUp).Cells(1, 26).Select
ActiveSheet.Paste
End With
Set wordDoc = Nothing
End Sub

Resources