preventing spreadsheet closing

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Preventing a spreadsheet from closing

Preventing a spreadsheet from closing

resolvedResolved · Medium Priority · Version 2013

Preventing a spreadsheet from closing

Hi Jens (or other trainer);

Would it be possible to have code in VBA that prevents the closing of a workbook if the file-size is over a certain amount or if there are more than xxxxx columns or rows?

Many thanks,
Amanda

RE: Preventing a spreadsheet from closing

Hi Amanda,

Thank you for the forum question.


Yes everything is possible in Excel (almost).

Create a before close event in ThisWorkBook as below:


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim LResult As Long

LResult = FileLen(ThisWorkbook.FullName)

MsgBox LResult


If LResult > 200000 Then
Cancel = True
End If

End Sub

You will not need the line MsgBox LResult, but this will tell you the size of the file in a message box. You will need to decide the max size. In my code above I have sat the max size to 200000 byte.

Good luck but let me know if you need more help.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Navigate with keyboard without losing your active cell

If you like using your keyboard to scroll through your excel document, but want to keep your active cell the same, use the scroll lock, and then use your arrow keys to navigate around the document.

View all Excel hints and tips


Server loaded in 0.07 secs.