I have created a couple of macros and I have a problem (sometimes) when I have several Excel files open. For some reason the macros stop working (VBA Runtime error 1004). I suspect that I have to declare workbook/worksheet properly, but I have yet to understand how. With my basic understand I thought the following would work (not in my code, I tried it and it didn't work): `
Dim wb as WorkBook
Dim ws as WorkSheet
Set wb = ActiveWorkbook
Set ws = ActiveWorkSheet
wb.ws.range("A1") = 2
`
However, since the above is not working for me I guess I'm missing some useful/valuable knowledge.
If useful: the macros are used for creating monthly report. Hence they are initially empty and then create a sheet for January, February and so forth.
Sub ConsultantInvoicing()
Dim CurrentMonth As String
Dim NextMonth As String
'Dim wb As Workbook
'Set wb = ActiveWorkbook
'Dim wsActiveSheet As Worksheet
'Set wsActiveSheet = wb.ActiveSheet
Sheets(Sheets.Count).Select
If Range("A1") = "" Then
Call AddFirstSheet
Else
Call AdditonalSheet
End If
End Sub
Private Sub AddFirstSheet()
CurrentMonth = Application.InputBox("Please enter what month",
"Invoice (monthly) - What month?")
ActiveSheet.Name = CurrentMonth
Call NewSheetFormat
End Sub
Private Sub AdditonalSheet()
CurrentMonth = Range("A1")
Select Case CurrentMonth
Case "January", "january"
NextMonth = "February"
Case "February", "february"
NextMonth = "March"
Case "March", "march"
NextMonth = "April"
Case "April", "april"
NextMonth = "May"
Case "May", "may"
NextMonth = "June"
Case "June", "june"
NextMonth = "July"
Case "July", "july"
NextMonth = "August"
Case "August", "august"
NextMonth = "September"
Case "September", "september"
NextMonth = "October"
Case "October", "october"
NextMonth = "November"
Case "November", "november"
v = "December"
Case "December", "december"
NextMonth = "January"
End Select
Set ws = ThisWorkbook.Sheets.Add _
(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ActiveSheet.Name = NextMonth
ActiveSheet.Range("A1") = NextMonth
Call NewSheetFormat
End Sub
Private Sub NewSheetFormat()
'formats the sheet
Range("A:A").ColumnWidth = 29.17
Range("ColumnWidth").ColumnWidth = 12.5
..
..
End Sub
Thanks in advance! Regards, Alexander