| VBA - The Excel Working Partner | |||||||||||||||||||||||||
| Learning From Your Code Sample | |||||||||||||||||||||||||
The resulting VBA program on the previous page has three statements in it. (We don't show the entire text of the recorded code since your version might be slightly different. You should be able to see the code in your VBA Editor code window.)
While this program will do the job, it still has at least one serious problem. If you Run it a second time, the program attempts to save another copy of the same XLS workbook on top of the first one and displays a confirmation dialog asking if you want to overwrite it. What you want is something more like what Excel itself does. In Excel, if you select File > New and Book1.xls already exists, Excel will create a workbook file as Book2.xls. So ... how can we modify our program to work the way we want it to? One way would be to check all the files in the current folder and create a string for the name of a new file that is different. This might be the most versatile way, but it could also require quite a bit of code complexity to program. We're going to do this a different and simpler way ... and illustrate an important point about the Personal workbook while we do it. The main purpose of the Personal workbook is to provide a place to store your "global" VBA programs. But it's also a regular workbook and since we have to have one anyway to save our VBA program, let's use the spreadsheet part of it to save the sequence number of our custom file. To accomplish this, we have to "Unhide" the workbook. Select Window > Unhide ... and then PERSONAL.XLS. The Personal.xls workbook should then be visible. I simply added a description and the value 0 in a cell. (I also changed the name of the worksheet to AboutParms mainly to add some documentation to the program.)
|
|||||||||||||||||||||||||


