The Save New Details button
To add a new entry onto the spreadsheet, the Save New Details
button needs to be clicked. The coding for this is fairly straightforward as we're only transferring the form details to cells on the spreadsheet. We need to do some error checking, however, just to make sure the correct data gets transferred.
Return to your form and double click your cmdSave
button to open up a code stub.
The first thing we need to do is to select the next empty row on the spreadsheet. If you have a look it again you'll see that this is row 6 for us:
After data gets added to row 6, the next empty row will be row 7, and so on.
Here's the line of code that selects the first empty cell in Column A:
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
Cells(LastRow + 1, 1).Select
You've met this code before, in a previous section. It searches for the last row on the spreadsheet that has data in it. This row number is then stored in the LastRow
variable. The second line then uses this row number between the round brackets of Cells
. We need to add 1 to the LastRow
variable to get the next empty row down.