My recorded macro errs sometimes because the file is open already (See macro below).
Can somebody tell me how to change the macro to first check if the Excel worksheet (Volledige Ice export.xlsx) is open, and if it is open, close the file first? With regards to the latter, I see I can do that with:
Windows("Volledige Ice export.xlsx").Activate
ActiveWorkbook.Close
The full macro:
Sub Ice()
Workbooks.OpenText Filename:="C:\Users\Doe\Documents\Ice\export.txt", _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 2), _
Array(2, 2), Array(3, 2), Array(4, 2), Array(5, 2), Array(6, 2), Array(7, 9), Array(8, 9), _
Array(9, 2), Array(10, 1), Array(11, 9), Array(12, 1), Array(13, 9), Array(14, 9), Array(15 _
, 9), Array(16, 9), Array(17, 9), Array(18, 9), Array(19, 9), Array(20, 9), Array(21, 2), _
Array(22, 9), Array(23, 9), Array(24, 2), Array(25, 2)), DecimalSeparator:=".", _
ThousandsSeparator:=",", TrailingMinusNumbers:=True
Windows("Ice_macro.xlsm").Activate
Windows("export.txt").Activate
Columns("A:B").Select
Selection.ColumnWidth = 21
Columns("D:D").Select
Selection.ColumnWidth = 18
Columns("E:E").Select
Selection.ColumnWidth = 15
Columns("F:F").Select
Selection.ColumnWidth = 60
Columns("G:G").Select
Selection.ColumnWidth = 45
Columns("H:H").Select
Selection.ColumnWidth = 12
Columns("L:L").Select
Selection.ColumnWidth = 45
Columns("G:G").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("A1").Select
Selection.AutoFilter
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\Doe\Documents\Ice\Volledige Ice export.xlsx", FileFormat _
:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close
End Sub