chaostheory
Registered User.
- Local time
- Today, 00:21
- Joined
- Sep 30, 2008
- Messages
- 69
Ok so we have a spreadsheet with like 15 tabs, oodles of code and triggers and subs that run when you change tabs etc to pull data from databases to populate drop down menus. At any rate...the file is 5mb and we will be generating hundreds of these. So to cut down on space i wrote a macro to run through the spreadsheet and delete all unused tabs which worked great. But the problem is we have code in the This.Workbook that now goes defunct cause most of the tabs done exist. I googled around a lot to try and find out how to delete code and i have one working. But there is a weird problem. It always errors out the first time run, then when you rerun it...it runs perfect. Im racking my brain and can't figure it out, it's stuff i have never dealt with before.
Code:
Sub saveDeleteWorkbookCode()
Dim x As Integer
[B]With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule[/B]
[B].DeleteLines 1, .CountOfLines[/B]
[COLOR=red]I get two different errors depending if i try to debug or just run it.[/COLOR]
[COLOR=red]This line gives me a "Can't enter break mode at this time" when i try to F8[/COLOR]
[COLOR=red]through it. But it did delete the code from This.Workbook like it was [/COLOR]
[COLOR=red]supposed to. But the error halts it every time.[/COLOR]
[COLOR=#ff0000]If i insert the code and just run it. I get a "compile error, expected end [/COLOR]
[COLOR=#ff0000]property" but once again, it does delete the code, then if i rerun it, it runs[/COLOR]
[COLOR=#ff0000]perfectly to end because the code is gone.[/COLOR]
End With
Call saveOptimization
End Sub
Sub saveOptimization()
Dim R As Integer, C As Integer
Dim theForm As String, formName As String
theForm = Sheets("Main Menu").Range("C3")
Application.DisplayAlerts = False
Worksheets("Form Info").Visible = True
Worksheets("Form Info").Activate
For R = 1 To ActiveSheet.UsedRange.Rows.count
If Range("A1").Offset(R, 0) = "Form Selection on Main Menu" Then
titleRow = R
End If
If Range("A1").Offset(R, 0) = theForm Then
For C = 1 To ActiveSheet.UsedRange.Columns.count
Worksheets("Form Info").Activate
formName = Range("A1").Offset(titleRow, C)
If Range("A1").Offset(R, C).Value <> "X" And Range("A1").Offset(R, C).Value <> "M" Then
If formName = "Form Info" Then
Exit For
End If
Worksheets(formName).Delete
End If
If Range("A1").Offset(R, C).Value = "X" Or Range("A1").Offset(R, C).Value = "M" Then
Worksheets(formName).Activate
ActiveSheet.Unprotect
Range("A1:IV30000").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
ActiveSheet.Protect
End If
Next C
End If
If formName = "Form Info" Then
Exit For
End If
Next R
Worksheets("Save").Activate
ActiveWorkbook.Save