detangler
Board Regular
- Joined
- Oct 21, 2003
- Messages
- 74
Hi All! I really need a lot of help here. Months ago I received generous help from someone at this forum with VBA codes that would allow me to add, sort, delete worksheets based on a list of departments specified in a worksheet called "Index".
I have since added bits and pieces of codes that do just a little more. Here's what I currently have:
I have at least 3 problems listed in order of importance:
1. This macro takes way too long to execute. Maybe the codes could be made more efficient. Maybe because the template worksheet ("DeptIS") that is used for copying new sheets has way too many CSE formulas in it that I'm currently using to populate data. It's hard to quantify the wait time but it's long enough time to make anyone thinks that it's crashing the PC.
--> Need help in making existing codes more efficient.
2. I'm currently getting the data from another workbook. In the data workbook, I have created several MS queries for the workbook to group relevant data together. Let's say I have a worksheet for each year of data and I need YTD plus 3 years of historical numbers. I create the same 4 sheets in the above model workbook and link it to the identical sheets in the data workbook. Then I use a bunch of array formulas (e.g. {=sum((dept="admin")*(acct="salaries")*end_bal)} to populate individual cells in each worksheet as it is generated by the above macros.
--> Need help in replacing the existing array formulas with VBA codes.
3. As more sheets are created, I get the 1004 run-time error. I did some researching and discovered that it is a known problem. I have looked at the workaround codes posted on the Microsoft KB and I have yet to adapt to my existing codes. I'm still working on this piece.
--> http://support.microsoft.com/default.aspx?scid=kb;en-us;210684
Any with any of the above would be greatly appreciated!!
Virginia
I have since added bits and pieces of codes that do just a little more. Here's what I currently have:
Code:
Sub MakeDeptSheets()
Dim SheetExists As Boolean
Dim LastDept As Integer, i As Integer, j As Integer
Dim NewDeptName As String, NewDeptLName As String, Msg As String, Title As String
Dim UReply As String, FName As String
UReply = MsgBox("Your workbook must be saved before you can proceed further. Save workbook now?", _
vbOKCancel + vbQuestion, "Save Workbook Now?")
Select Case UReply
Case vbCancel
MsgBox "You will not be able to use the Update Statements feature.", vbInformation, "Sorry..."
Exit Sub
Case vbOK
Application.ScreenUpdating = False
Application.DisplayAlerts = False
FName = "Financial_Model_" & Format(Now, "mmddyy")
Application.Dialogs(xlDialogSaveAs).Show FName
Application.StatusBar = "Please wait while the model is being updated..."
Sheets("Start").Visible = True
Sheets("End").Visible = True
On Error GoTo Xit
With ThisWorkbook.Worksheets("Index")
' Assuming 6 header rows and first department appears on row 7
' Sorting departments on Index, allowing up to 60 entries
.Range("A7:AC66").Select
Selection.Sort Key1:=Range("C7"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
LastDept = .Range("B65536").End(xlUp).Row
' Looking to add sheets here and looping through all entries on Index
' Assuming 6 header rows and first department appears on row 7
For i = 7 To LastDept
SheetExists = False
For j = 1 To Worksheets.Count
' If true, then sheet exists and exits loop
' MsgBox Sheets(j).Name & " - " & .Cells(i, 3)
If (Sheets(j).Name = .Cells(i, 3)) Then
SheetExists = True
Exit For
End If
Next j
If (SheetExists = False) Then
NewDeptName = .Cells(i, 3)
NewDeptLName = .Cells(i, 2)
' Assuming 6 header rows and first department appears on row 7
Sheets("DeptIS").Copy Before:=Sheets("End")
With ActiveSheet
.Name = NewDeptName
.[B5].Value = NewDeptLName
.Visible = True
End With
End If
Next i
' Looking for sheets which, not being listed in Index, should be deleted here
ReStart:
For i = 1 To Worksheets.Count
SheetExists = False
' Making exceptions to sheets not on Index that should not be deleted
If (Sheets(i).Name = "Input") Or (Sheets(i).Name = "Index") Or _
(Sheets(i).Name = "Inflation") Or (Sheets(i).Name = "IPRollup") Or _
(Sheets(i).Name = "OPRollup") Or (Sheets(i).Name = "FinStmt") Or _
(Sheets(i).Name = "DeptIS") Or (Sheets(i).Name = "Start") Or _
(Sheets(i).Name = "End") Or (Sheets(i).Name = "YTD") Then
SheetExists = True
Else
' Assuming 6 header rows and first department appears on row 7
For j = 7 To LastDept
If (Sheets(i).Name = .Cells(j, 3)) Then
SheetExists = True
Exit For
End If
Next j
If (SheetExists = False) Then
Sheets(i).Delete
GoTo ReStart
End If
End If
Next i
End With
Xit:
Sheets("Start").Visible = False
Sheets("End").Visible = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.StatusBar = False
Sheets("Index").Activate
Range("A1").Select
' ***Need more consideration on this Save***
ActiveWorkbook.Save
If Err.Number <> 0 Then
ErrMsg = "Error:" & Str(Err.Number) & " was generated. " _
& Err.Source & Chr(13) & Err.Description
MsgBox ErrMsg, , "Ooops...", Err.HelpFile, Err.HelpContext
MsgBox "Processing will now be terminated. Please save your work."
Err.Clear
Else
MsgBox "All departmental income statements have been updated."
End If
End Select
End Sub
I have at least 3 problems listed in order of importance:
1. This macro takes way too long to execute. Maybe the codes could be made more efficient. Maybe because the template worksheet ("DeptIS") that is used for copying new sheets has way too many CSE formulas in it that I'm currently using to populate data. It's hard to quantify the wait time but it's long enough time to make anyone thinks that it's crashing the PC.
--> Need help in making existing codes more efficient.
2. I'm currently getting the data from another workbook. In the data workbook, I have created several MS queries for the workbook to group relevant data together. Let's say I have a worksheet for each year of data and I need YTD plus 3 years of historical numbers. I create the same 4 sheets in the above model workbook and link it to the identical sheets in the data workbook. Then I use a bunch of array formulas (e.g. {=sum((dept="admin")*(acct="salaries")*end_bal)} to populate individual cells in each worksheet as it is generated by the above macros.
--> Need help in replacing the existing array formulas with VBA codes.
3. As more sheets are created, I get the 1004 run-time error. I did some researching and discovered that it is a known problem. I have looked at the workaround codes posted on the Microsoft KB and I have yet to adapt to my existing codes. I'm still working on this piece.
--> http://support.microsoft.com/default.aspx?scid=kb;en-us;210684
Any with any of the above would be greatly appreciated!!
Virginia