Save and Close Button


Posted by Sean H. on October 17, 2001 10:47 AM

Can somebody please give me step by step instructions on how to make a "save and close" button in an excel worksheet?

Thank you kindly in advance,
Sean

Posted by Jonathan on October 17, 2001 5:32 PM


OK. You say "in an excel worksheet" so that's where it'll go :)
Be on the sheet you want to have this button. Find the VisualBasic Toolbar: View/Toolbars/VisualBasic.

There's an icon on the VB toolbar that looks like a hammer crossed with a crescent wrench. Click that to open the "toolbox".

OK. In the toolbox you can "hover" over an element to get the tooltip on what it is. There's one that looks like a timny button. Hover your mouse pointer over it and it should say "Command Button". OK. Click that and then draw the button where you want it on the sheet: you can make it any size you like.

OK. Now rightclick over the newly drawn button. Pick Properties from the Pop-up menu. There are two properties I would change. The Name property I change to cmdSaveNClose (but you don't have to do this; still it's a good habit to get into). The second property I change is the Caption property. I change it to "Save and Close" (without the quotation marks). You will see, if you look around in the properties that you can change the font of the caption, the color of the caption, and a few other things if you like. OK.

Close the Properties Window and rightclick over the newly captioned button and this time pick ViewCode from the pop-up menu. The Visual Basic Editor opens and there is already the "stub" of an event handler. It says

Private Sub cmdSaveNClose_Click()

End Sub

Alright. All we're going to do is to write two lines of code into this 'stub'. (If you really get into this stuff, you'll want to write some extra lines of code to handle any errors that may ever occur, but this is a simple event handler and the chances are you can do without it, for your own use). Here's the two lines we add Between the two lines that are already there:

ThisWorkbook.Save
ThisWorkbook.Close

So the whole thing now looks like this:

Private Sub cmdSaveNClose_Click()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub

(It's a good idea to indent your code like this to make it easier to understand.)

OK. Still in the Visual Basic Editor choose File/Close and Return to Microsoft Excel.

You'll want to close the toolbox (close button in upper right of toolbox). On your Visual Basic toolbar you'll see that there's an icon that looks like a ruler and a triangle, maybe a T-square. It's still depressed (meaning we're still in Design mode. OK. Click it once and we're back to our sheet in normal mode and the new button is on the sheet. Now, if you want, oyu can hide the Visual Basic toolbar again by View/Toolbars/ and unselecting it.

Try the button. Write something new on the sheet and then hit the button. It should save and close the workbook. Then open it up and see if the last thing you wrote was saved.

Good luck!

Posted by Sean H. on October 19, 2001 6:16 AM

Thank you very much Jonathan!



Posted by Jonathan on October 19, 2001 6:20 PM


No problem!

Thank you very much Jonathan! :