DKRbella0814
Board Regular
- Joined
- Aug 10, 2008
- Messages
- 155
Each morning, I pull electronic production data (from CNC machines) onto a MBD viewer (access database viewer) and export the filtered data to excel, saving the file as a CSV file.
Since this is a repetitive task, I would like to write a macro that formats the data within the CSV file into its desired form.
Because I am downloading a new CSV file each day, I figured that the best way to go about programming a macro with the desired formatting steps was to record and save the macro in a blank workbook. Then, each morning when I want to reformat the new CSV file, I would also open the XLS file (containing the formatting macro) and run the macro in the CSV workbook.
However, this approach has not worked for me and I am not sure if this is due to the fact that the macro file is a XLS file and the file needing formatted is a CSV file.
The steps which need completed in order to reformat the CSV file upon opening the file are listed below:
1) Select Row 1 and insert row
2) Select Cols A:N and autofit
3) Select Col D and insert column
4) Select Col F and insert column
5) Select Col G and insert column
6) Select Col C - Select 'Data' menu - Select 'Text to Columns' - Select 'Fixed Width' - Adjust to 10' - Finish
7) Select Col E - Select 'Data' menu - Select 'Text to Columns' - Select 'Fixed Width' - Adjust to 10' - Finish
8) Select Col C and E - Reformat Dates to mm/dd/yy and 'Center' justify
9) In row 1, insert Col headers:
B = Machine
C = Cycle Start Dt
D = Cycle start Time
E = Cycle End Date
F = Cycle End Time
G = Cycle Time (Min)
I = Good Cycles (True or False)
J = Downtime between Cycles
K = Description (JN and Operation)
10) In Col G - the following formula needs written and copied down to G5000 and then reformatted to time format hh:mm
=if(or(isblank(F2), isblank(D2), iserror(F2-D2)), " ", (F2-D2))
11) In Col J - the following formula needs written and copied down to J5000 and then reformatted to time format hh:mm
=if(or(isblank(D3), isblank(F2), iserror(D3-F2)), " ", (D3-F2))
12) In col I (Good Cycles), Conditional format the cells in col I:2 to I:5000 to do the following:
If active cell in Col I = TRUE , format fill Green
If active cell in Col I = FALSE, format fill RED
If active cell is blank = do not format
13) Select col A:N and auto fit
This is the macro code which I have tried to write but is not working. Can someone please help me out???
Sub Format()
'
' Format Macro
' Macro recorded 5/18/2011 by BMC
'
'
Rows("1:1").Select
Selection.insert Shift:=xlDown
Columns("A:N").Select
Columns("A:N").EntireColumn.AutoFit
Columns("D:D").Select
Selection.insert Shift:=xlToRight
Columns("G:G").Select
Selection.insert Shift:=xlToRight
Columns("F:F").Select
Selection.insert Shift:=xlToRight
Columns("C:C").Select
Selection.TextToColumns Destination:=Range("c1:D1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(10, 1), Array(19, 1)), TrailingMinusNumbers _
:=True
Columns("E:E").Select
Selection.TextToColumns Destination:=Range("e1:F1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(10, 1), Array(19, 1)), TrailingMinusNumbers _
:=True
Selection.NumberFormat = "mm/dd/yy;@"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("C:C").Select
Selection.NumberFormat = "mm/dd/yy;@"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("B1").Select
ActiveCell.FormulaR1C1 = "Machine"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Cycle St. Dt."
Range("D1").Select
ActiveCell.FormulaR1C1 = "Cycle St. Time"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Cycle End Dt."
Range("F1").Select
ActiveCell.FormulaR1C1 = "Cycle End Time"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Cycle Time (Min)"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Good Cycles"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Downtime"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Description"
Columns("G:G").ColumnWidth = 7.29
ActiveCell.FormulaR1C1 = "Cycle Time (Min)"
Columns("A:N").Select
Columns("A:N").EntireColumn.AutoFit
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(ISBLANK(RC[-3]),ISBLANK(RC[-1])),"""",ABS(RC[-1]-RC[-3]))"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G5000"), Type:=xlFillDefault
Range("G2:G5000").Select
Columns("G:G").Select
Selection.NumberFormat = "h:mm;@"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("I2").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="TRUE"
Selection.FormatConditions(1).Interior.ColorIndex = 4
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=FALSE"
Selection.FormatConditions(2).Interior.ColorIndex = 3
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK(I2)"
Selection.Copy
Range("I3:I5000").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("J2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("J3").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(ISBLANK(RC[-6]),ISBLANK(R[-1]C[-4]),ISERROR(RC[-6]-R[-1]C[-4])),"""",(RC[-6]-R[-1]C[-4]))"
Range("J3").Select
Selection.AutoFill Destination:=Range("J3:J4999"), Type:=xlFillDefault
Range("J3:J4999").Select
Range("N4997").Select
End Sub
Since this is a repetitive task, I would like to write a macro that formats the data within the CSV file into its desired form.
Because I am downloading a new CSV file each day, I figured that the best way to go about programming a macro with the desired formatting steps was to record and save the macro in a blank workbook. Then, each morning when I want to reformat the new CSV file, I would also open the XLS file (containing the formatting macro) and run the macro in the CSV workbook.
However, this approach has not worked for me and I am not sure if this is due to the fact that the macro file is a XLS file and the file needing formatted is a CSV file.
The steps which need completed in order to reformat the CSV file upon opening the file are listed below:
1) Select Row 1 and insert row
2) Select Cols A:N and autofit
3) Select Col D and insert column
4) Select Col F and insert column
5) Select Col G and insert column
6) Select Col C - Select 'Data' menu - Select 'Text to Columns' - Select 'Fixed Width' - Adjust to 10' - Finish
7) Select Col E - Select 'Data' menu - Select 'Text to Columns' - Select 'Fixed Width' - Adjust to 10' - Finish
8) Select Col C and E - Reformat Dates to mm/dd/yy and 'Center' justify
9) In row 1, insert Col headers:
B = Machine
C = Cycle Start Dt
D = Cycle start Time
E = Cycle End Date
F = Cycle End Time
G = Cycle Time (Min)
I = Good Cycles (True or False)
J = Downtime between Cycles
K = Description (JN and Operation)
10) In Col G - the following formula needs written and copied down to G5000 and then reformatted to time format hh:mm
=if(or(isblank(F2), isblank(D2), iserror(F2-D2)), " ", (F2-D2))
11) In Col J - the following formula needs written and copied down to J5000 and then reformatted to time format hh:mm
=if(or(isblank(D3), isblank(F2), iserror(D3-F2)), " ", (D3-F2))
12) In col I (Good Cycles), Conditional format the cells in col I:2 to I:5000 to do the following:
If active cell in Col I = TRUE , format fill Green
If active cell in Col I = FALSE, format fill RED
If active cell is blank = do not format
13) Select col A:N and auto fit
This is the macro code which I have tried to write but is not working. Can someone please help me out???
Sub Format()
'
' Format Macro
' Macro recorded 5/18/2011 by BMC
'
'
Rows("1:1").Select
Selection.insert Shift:=xlDown
Columns("A:N").Select
Columns("A:N").EntireColumn.AutoFit
Columns("D:D").Select
Selection.insert Shift:=xlToRight
Columns("G:G").Select
Selection.insert Shift:=xlToRight
Columns("F:F").Select
Selection.insert Shift:=xlToRight
Columns("C:C").Select
Selection.TextToColumns Destination:=Range("c1:D1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(10, 1), Array(19, 1)), TrailingMinusNumbers _
:=True
Columns("E:E").Select
Selection.TextToColumns Destination:=Range("e1:F1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(10, 1), Array(19, 1)), TrailingMinusNumbers _
:=True
Selection.NumberFormat = "mm/dd/yy;@"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("C:C").Select
Selection.NumberFormat = "mm/dd/yy;@"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("B1").Select
ActiveCell.FormulaR1C1 = "Machine"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Cycle St. Dt."
Range("D1").Select
ActiveCell.FormulaR1C1 = "Cycle St. Time"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Cycle End Dt."
Range("F1").Select
ActiveCell.FormulaR1C1 = "Cycle End Time"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Cycle Time (Min)"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Good Cycles"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Downtime"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Description"
Columns("G:G").ColumnWidth = 7.29
ActiveCell.FormulaR1C1 = "Cycle Time (Min)"
Columns("A:N").Select
Columns("A:N").EntireColumn.AutoFit
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(ISBLANK(RC[-3]),ISBLANK(RC[-1])),"""",ABS(RC[-1]-RC[-3]))"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G5000"), Type:=xlFillDefault
Range("G2:G5000").Select
Columns("G:G").Select
Selection.NumberFormat = "h:mm;@"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("I2").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="TRUE"
Selection.FormatConditions(1).Interior.ColorIndex = 4
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=FALSE"
Selection.FormatConditions(2).Interior.ColorIndex = 3
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK(I2)"
Selection.Copy
Range("I3:I5000").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("J2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("J3").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(ISBLANK(RC[-6]),ISBLANK(R[-1]C[-4]),ISERROR(RC[-6]-R[-1]C[-4])),"""",(RC[-6]-R[-1]C[-4]))"
Range("J3").Select
Selection.AutoFill Destination:=Range("J3:J4999"), Type:=xlFillDefault
Range("J3:J4999").Select
Range("N4997").Select
End Sub