K
Keith
I have been spending many days trying to figure out what part of the
code is not working correctly. I am using Excel 2003. I followed the
instructions on the Ozgrid website (listed below).
http://www.ozgrid.com/VBA/delete-sheet-code.htm
Sub DeleteSheetEventCode()
''Needs Reference Set To _
"Microsoft Visual Basic For Applications Extensibility"
'Tools>References.
Dim sSheet As Object, strName As String
For Each sSheet In Sheets
Select Case UCase(sSheet.Name)
Case "SHEET1", "SHEET2", "SHEET3"
strName = sSheet.CodeName
With ThisWorkbook.VBProject.VBComponents
(strName).CodeModule
.DeleteLines 1, .CountOfLines
End With
Case Else
'Whatever
End Select
Next sSheet
End Sub
I have checked the checkbox for Tools>>Reference>>Microsoft Visual
Basic for Applications Extensibility 5.3 in the VBA Project.
I also have checked the checkbox for
Tools>>Macro>>Security...>>Trusted Publishers>>Trust Access to Visual
Basic Project.
I run this macro and it works. I try and run it in break mode and it
will go through two loops and then provide an error on the third. I
call the macro, and it freezes inside of this macro (it never
completes).
I have tried this as a separate macro and called it, and also as part
of the regular macro. I need to run code like this as my originating
worksheet is running the Worksheet_Change event macro and is causing
errors when this code is copied.
I am listing below the pertinent snippet of my code.
=================================================================================
Windows(DSCName).Activate ' This brings
the focus to the workbook.
shts = Application.Sheets.count ' Count the
number of sheets in the workbook.
count = 2 ' Sets the
default. This is a counter for the number of sheets being cleared.
For Each Worksheet In Worksheets ' Reminder the
focus is currently on the DSC file when entering this loop.
Sheets(count).Activate
ActiveSheet.Copy After:=Workbooks(DWORName).Sheets(count) '
Copies the worksheet from the DSC file to the DWOR file.
Windows(DWORName).Activate ' Ensures the
focus is on the DWOR file.
' DeleteProcedureCode Workbooks(DWORName), DSCTab,
"Worksheet_Change" ' Deletes the Procedure in the Worksheet that is
activated when any changes is completed. This was just copied from
the DSC spreadsheet.
' Call DeleteSheetCode ' Deletes all
of the code in each of the sheets. The main intent is to remove code
copied from the DSC (such as the "Worksheet_Change" event).
'=======================================================================================
' Needs Tools>References Set To "Microsoft Visual Basic For
Applications Extensibility".
' This will delete all of the code from each sheet.
' This is required to ensure the "Worksheet_Change" event is deleted
when it is copied from the DSC file.
With ThisWorkbook.VBProject.VBComponents
(ActiveSheet.CodeName).CodeModule
.DeleteLines 1, .CountOfLines
End With
'=========================================================================================
Windows(DSCName).Activate ' Returns the
focus to the DSC file after having pasted the sheet to the DWOR file.
ActiveSheet.Range("Comments").Copy ' Copies the
data in the Comments section. This is done separately because it can
easily contain more than 255 characters and that is all that is copied
per cell when copying the sheet.
Windows(DWORName).Activate ' Moves the
focus to the DWOR file.
ActiveSheet.Paste Destination:=Range("Comments") ' Pastes the
Comments information from the DSC file to the DWOR file.
SendKeys ("^{HOME}") ' Ctrl+Home
Windows(DSCName).Activate ' Returns the
focus to the DSC file after having pasted the sheet to the DWOR file.
FirstDay = ActiveSheet.Range("WkDateMon").Value ' Copies the
data values in the Dates section. This is done separately because we
don't want the formula; we want to have the values.
Windows(DWORName).Activate ' Moves the
focus to the DWOR file.
With ActiveSheet
.Unprotect ' Unprotects
the worksheet on the DWOR file.
.Range("WkDateMon").Value = FirstDay ' Pastes the
Date values from the DSC file to the DWOR file. (This can be compared
to the dates in the DWOR then.)
End With
SendKeys ("^{HOME}") ' Ctrl+Home
Windows(DSCName).Activate
If count = shts Then ' Checks to
see if we have copied the last sheet in the DSC workook.
Workbooks(DSCName).Close SaveChanges:=False ' Closes the
DSC file.
Windows(DWORName).Activate ' Focus is
moved to the DWOR file.
End If
count = count + 1 ' This will
set the sheet number to 2 thereby skipping the first tab labeled
"Instructions".
Next
code is not working correctly. I am using Excel 2003. I followed the
instructions on the Ozgrid website (listed below).
http://www.ozgrid.com/VBA/delete-sheet-code.htm
Sub DeleteSheetEventCode()
''Needs Reference Set To _
"Microsoft Visual Basic For Applications Extensibility"
'Tools>References.
Dim sSheet As Object, strName As String
For Each sSheet In Sheets
Select Case UCase(sSheet.Name)
Case "SHEET1", "SHEET2", "SHEET3"
strName = sSheet.CodeName
With ThisWorkbook.VBProject.VBComponents
(strName).CodeModule
.DeleteLines 1, .CountOfLines
End With
Case Else
'Whatever
End Select
Next sSheet
End Sub
I have checked the checkbox for Tools>>Reference>>Microsoft Visual
Basic for Applications Extensibility 5.3 in the VBA Project.
I also have checked the checkbox for
Tools>>Macro>>Security...>>Trusted Publishers>>Trust Access to Visual
Basic Project.
I run this macro and it works. I try and run it in break mode and it
will go through two loops and then provide an error on the third. I
call the macro, and it freezes inside of this macro (it never
completes).
I have tried this as a separate macro and called it, and also as part
of the regular macro. I need to run code like this as my originating
worksheet is running the Worksheet_Change event macro and is causing
errors when this code is copied.
I am listing below the pertinent snippet of my code.
=================================================================================
Windows(DSCName).Activate ' This brings
the focus to the workbook.
shts = Application.Sheets.count ' Count the
number of sheets in the workbook.
count = 2 ' Sets the
default. This is a counter for the number of sheets being cleared.
For Each Worksheet In Worksheets ' Reminder the
focus is currently on the DSC file when entering this loop.
Sheets(count).Activate
ActiveSheet.Copy After:=Workbooks(DWORName).Sheets(count) '
Copies the worksheet from the DSC file to the DWOR file.
Windows(DWORName).Activate ' Ensures the
focus is on the DWOR file.
' DeleteProcedureCode Workbooks(DWORName), DSCTab,
"Worksheet_Change" ' Deletes the Procedure in the Worksheet that is
activated when any changes is completed. This was just copied from
the DSC spreadsheet.
' Call DeleteSheetCode ' Deletes all
of the code in each of the sheets. The main intent is to remove code
copied from the DSC (such as the "Worksheet_Change" event).
'=======================================================================================
' Needs Tools>References Set To "Microsoft Visual Basic For
Applications Extensibility".
' This will delete all of the code from each sheet.
' This is required to ensure the "Worksheet_Change" event is deleted
when it is copied from the DSC file.
With ThisWorkbook.VBProject.VBComponents
(ActiveSheet.CodeName).CodeModule
.DeleteLines 1, .CountOfLines
End With
'=========================================================================================
Windows(DSCName).Activate ' Returns the
focus to the DSC file after having pasted the sheet to the DWOR file.
ActiveSheet.Range("Comments").Copy ' Copies the
data in the Comments section. This is done separately because it can
easily contain more than 255 characters and that is all that is copied
per cell when copying the sheet.
Windows(DWORName).Activate ' Moves the
focus to the DWOR file.
ActiveSheet.Paste Destination:=Range("Comments") ' Pastes the
Comments information from the DSC file to the DWOR file.
SendKeys ("^{HOME}") ' Ctrl+Home
Windows(DSCName).Activate ' Returns the
focus to the DSC file after having pasted the sheet to the DWOR file.
FirstDay = ActiveSheet.Range("WkDateMon").Value ' Copies the
data values in the Dates section. This is done separately because we
don't want the formula; we want to have the values.
Windows(DWORName).Activate ' Moves the
focus to the DWOR file.
With ActiveSheet
.Unprotect ' Unprotects
the worksheet on the DWOR file.
.Range("WkDateMon").Value = FirstDay ' Pastes the
Date values from the DSC file to the DWOR file. (This can be compared
to the dates in the DWOR then.)
End With
SendKeys ("^{HOME}") ' Ctrl+Home
Windows(DSCName).Activate
If count = shts Then ' Checks to
see if we have copied the last sheet in the DSC workook.
Workbooks(DSCName).Close SaveChanges:=False ' Closes the
DSC file.
Windows(DWORName).Activate ' Focus is
moved to the DWOR file.
End If
count = count + 1 ' This will
set the sheet number to 2 thereby skipping the first tab labeled
"Instructions".
Next