Hi
I recieve a spreadsheet daily which I then apply a macro to do do various bits and bobs. What I have found is that the spreadsheet does occasionally change in format or layout - for example very occasionally a new column is added. This makes my macro fall over.
Is there a way of the macro first performing a check to ensure that the format is exactly as it should be? or if there has been a change in columns to issue some sort of warning? Macro below if that helps
I recieve a spreadsheet daily which I then apply a macro to do do various bits and bobs. What I have found is that the spreadsheet does occasionally change in format or layout - for example very occasionally a new column is added. This makes my macro fall over.
Is there a way of the macro first performing a check to ensure that the format is exactly as it should be? or if there has been a change in columns to issue some sort of warning? Macro below if that helps
Code:
Sub Dublin()
'
Application.WindowState = xlMinimized
'remove formats and cellmerge etc
Cells.Select
Selection.ClearFormats
'deletes first row to remove heading
Rows("1:1").Select
Selection.Delete Shift:=xlUp
'removes any hidden rows
Cells.Select
Cells.EntireRow.AutoFit
'formats premium column as numeric
Columns("J:J").Select
Selection.NumberFormat = "0.00"
'names column U1 "date"
Range("U1").Select
ActiveCell.FormulaR1C1 = "date"
Range("V1").Select
ActiveCell.FormulaR1C1 = "dummy"
'extracts the date from the Dublin sheet
Range("AA3").Formula = "=MID(CELL(""filename"",A1),FIND(""["",CELL(""filename"",A1))+1,LEN(CELL(""filename"",A1))-FIND(""]"",CELL(""filename"",A1))+11)"
Range("AA4").Formula = "=RIGHT(AA3,10)"
With Range("AA5")
.NumberFormat = "mm/dd/yyyy"
.Formula = "=SUBSTITUTE(AA4,"" "",""/"")+0"
End With
'adds extracted date into new date column "U"
Range("AA5").Select
Selection.Copy
Range(Range("U" & Rows.Count).End(xlUp).Offset(1, 0), Range("T" & Rows.Count).End(xlUp).Offset(0, 1)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("U2").Select
Selection.Copy
Range(Range("V" & Rows.Count).End(xlUp).Offset(1, 0), Range("U" & Rows.Count).End(xlUp).Offset(0, 1)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'format column to date
Columns("U:U").Select
Selection.NumberFormat = "mm/dd/yyyy"
'format x column
Columns("V:V").Select
Selection.NumberFormat = "General"
'remove workings
Columns("AA:AA").Select
Selection.Delete Shift:=xlToLeft
Application.DisplayAlerts = False
'save in Dublin Folder
With ActiveWorkbook
.SaveAs Filename:="C:\Blah" & .Name
End With
'save as CSV for SAS import
With ActiveWorkbook
.SaveAs Filename:="D:\Dublin.csv", FileFormat:= _
xlCSV, CreateBackup:=False
End With
Application.DisplayAlerts = True
ActiveWorkbook.Saved = True
ActiveWorkbook.Close
End Sub