I'm a novice (at best) in writing VBA code and I'm trying to "automate" a workbook for the "boss".
What happens is I have a workbook with a sheet for every month of the year that contains columns for ID, Name, Amnt, Prev Balance, Fees, Amnt Paid, Current Balnce. The ID, Name and Amnt come from an access database. As days go by paid amnts are entered and balances are calculated.
So I created this code to:
1. Copy the "values" of the cells into a new sheet
2. Create the new month sheet
3. Carry over the balances.
These sheets are created from a template (that is hidden) by a button on the sheet.
The previous balances are retreived from the previous month sheet with a formula link. And the current balance is a simple formula.
I have written this code that works, but it has "holes" in it. That is, if the user tries to create the sheet more than once within a month, it will error out. I have tried to catch this through code but I'm not there yet.
Here's what I have so far. . . most of which I received from this forum. . . Thanks all.
Can someone point me in the right direction to make this as foolproof (idiotproof) as possible.
I think I'm in that position of "when you're in a swamp full of alligators, it's hard to remember your objective was to drain the swamp".
Oh, and my "boss" is my wife. . . ie the alligators
What happens is I have a workbook with a sheet for every month of the year that contains columns for ID, Name, Amnt, Prev Balance, Fees, Amnt Paid, Current Balnce. The ID, Name and Amnt come from an access database. As days go by paid amnts are entered and balances are calculated.
So I created this code to:
1. Copy the "values" of the cells into a new sheet
2. Create the new month sheet
3. Carry over the balances.
These sheets are created from a template (that is hidden) by a button on the sheet.
The previous balances are retreived from the previous month sheet with a formula link. And the current balance is a simple formula.
I have written this code that works, but it has "holes" in it. That is, if the user tries to create the sheet more than once within a month, it will error out. I have tried to catch this through code but I'm not there yet.
Here's what I have so far. . . most of which I received from this forum. . . Thanks all.
Code:
Option Explicit
Private Sub DoIt_Click()
Dim Sh, aWorkSheet, ws As Worksheet
Dim Shbak, wsName, strTextFile, strDate, wsNamecpy, txtDate, response, txtrandom, new_sheetname As String
Dim NewMnth, dtmDate As Date
Dim LastDayPrevMonth As Date
Dim tstDate As Integer
Dim i, j, k, w As Integer
'Test to see if it's the Last week of the month
tstDate = Format(Now, "dd")
If tstDate < 26 Then
response = MsgBox("Are You Sure You Want to Create the Next Month Sheet?", vbOKCancel + vbQuestion, "New Month Creation")
Else
End If
If response = 2 Then
'Exit Subroutine
GoTo tstStop
Else
'Set the variable for sheet names
wsName = ActiveSheet.Name
wsNamecpy = wsName & " (2)"
'Copy the worksheet and "values" of cells -- turn off autofilter
Worksheets(wsName).Copy Before:=Worksheets(wsName)
Worksheets(wsName).Activate
Range("a1").AutoFilter
ActiveSheet.Range("a2:K66").Select
ActiveSheet.Range("a3:K66").Select
Application.Selection.Copy
Worksheets(wsNamecpy).Activate
ActiveSheet.Range("A3:k66").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Range("A2").Select
'Rename the backup worksheet
Worksheets(wsName).Activate
Range("A2:k66").AutoFilter
Set Sh = Worksheets(wsName & " (2)")
Sh.Name = wsName & "BAK"
'Save backup worksheet name for later
Shbak = wsName & "BAK"
'Create New Sheet from RRTemplate
LastDayPrevMonth = DateSerial(Year(Date), Month(Date), 0)
dtmDate = LastDayPrevMonth
strDate = Format(dtmDate, "yyyy")
txtDate = Format(dtmDate + 1, "mmmm")
strDate = Right$(strDate, 2)
i = Format(Date + 1, "MM")
Worksheets("RRTemplate").Copy After:=Worksheets(wsName)
Worksheets(wsName).Activate
Set Sh = Worksheets("RRTemplate (2)")
new_sheetname = Format(dtmDate + 1, "MMM") & strDate
For w = 1 To ActiveWorkbook.Worksheets.Count
If Worksheets(w).Name = new_sheetname Then
response = MsgBox("There Is Already A " & new_sheetname & "Do you Want to Continue?", vbOKCancel + vbQuestion, "New Month Creation")
If response = 2 Then
GoTo tstStop
Else
End If
txtrandom = Str$(Int(6 * Rnd) + 1)
Sh.Name = Format(dtmDate + 1, "MMM") & strDate & txtrandom
Else
'do nothing
End If
Next
'%Create New Sheet from RRTemplate
If Sh.Name = "RRTemplate (2)" Then
Sh.Name = Format(dtmDate + 1, "MMM") & strDate
Else
End If
Worksheets(Sh.Name).Activate
'Put Month text in cell D1
ActiveSheet.Range("d1") = txtDate
'Turn autofilter on in new sheet and add button
ActiveSheet.Range("a2:k66").AutoFilter
Sheets(Sh.Name).Visible = True
'******** Because RRTemplate is hidden ********
'Insert formula for Previous balance
Worksheets(Sh.Name).Activate
ActiveSheet.Range("D3").Select
ActiveCell.FormulaR1C1 = "=SUM('" & Shbak & "'!RC[7])"
Worksheets(Sh.Name).Range("d3:d66").FillDown
GoTo Endit
tstStop:
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("RRTemplate (2)").Delete
Application.DisplayAlerts = True
Endit:
End If
End Sub
I think I'm in that position of "when you're in a swamp full of alligators, it's hard to remember your objective was to drain the swamp".
Oh, and my "boss" is my wife. . . ie the alligators