Wednesday, 12 October 2011

Excel MROUND giving errors in VBA code

I was recently working on an Excel spreadsheet with a VBA module that inserted formulas with the MROUND function.

range.formulaR1C1 = "=MROUND(RC[-1],.02)"

The MROUND function is quite handy, it takes the value of the calculation and rounds it to the nearest value specified in the second argument (as opposed to the ROUND() fucntion which jsut rounds to the nearest significant digit) It is found within the "Analysis Toolpak" Add-in.

A colleague ran the spreadsheet and the darned thing fell over. It turns out they also had the add-in "Analysis Tookpak - VBA" installed. When we turned it off, it ran fine so now the code turns it off automatically.



Sub Workbook_Open()

If AddIns("Analysis Toolpak - VBA").Installed = True Then

continue = msgBox("Turning off Analysis Tookpak - VBA",vbOkCancel)

If continue = 2 Then

Exit Sub

Else

AddIns("Analysis Toolpak - VBA").Installed = False

End If

End Sub

Thursday, 6 October 2011

Excel VBA: Application.GetSaveAsFilename

I discovered today that there is some undocumented protections built into the GetSaveAsFilename method. If you supply a filename with fullstops in it but without a valid extension then the filename that appears in the dialog box will be surrounded by quotes so make sure that you always append the correct filetype to the end of the filename if you are pulling the filename from a variable.