View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JLGWhiz[_2_] JLGWhiz[_2_] is offline
external usenet poster
 
Posts: 1,565
Default Deleting all code in new file only (creating using save as)

This is the key line in Chip's code for determining which workbook it
deletes from.

Set VBProj = ActiveWorkbook.VBProject

Change ActiveWorkbook to the name of your new workbook that you want the
code deleted from, or just make sure that it is the active workbook. Either
way that ties it down to where you want to do the delete.




"CB" wrote in message
...
Hi everyone,

Programming in Excel is rather new to me. I've been able to get by thus
far
by recording macros and looking at the code. I have been able to
manipulate
the code by referring to the VBE help and these news groups.

I'm looking for assistance in modifying my code such that when the file is
saved (SAVE AS actually), all code is removed from the new file but not
the
current file. I've searched these discussions groups and found a reference
to
http://www.cpearson.com/excel/VBE.aspx where the following code will
delete
all VBA in a project.

Sub DeleteAllVBACode()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule

Set VBProj = ActiveWorkbook.VBProject

For Each VBComp In VBProj.VBComponents
If VBComp.Type = vbext_ct_Document Then
Set CodeMod = VBComp.CodeModule
With CodeMod
.DeleteLines 1, .CountOfLines
End With
Else
VBProj.VBComponents.Remove VBComp
End If
Next VBComp
End Sub

What I am not clear on is whether or not this code will delete all code in
the workbook that is currently open or whether it will delete the code in
the
new workbook that is created when my code does a "save as". The first line
under the declarations makes me think perhaps it deletes the code in the
workbook currently opened.

Am I correct?

FWIW, I'm including the code that executes when my user clicks the command
button to save the file. Sorry in advance if the lines don't wrap
properly.

Private Sub SaveData_Click()

If Trim(Worksheets("Post-Service").Range("D3").Value = "") Then
MsgBox ("You must enter a serial number.")
Exit Sub
Else
Worksheets("Post-Service").Range("D3") =
UCase(Worksheets("Post-Service").Range("D3"))
If Left(Worksheets("Post-Service").Range("D3"), 1) = "C" Then
ActiveWorkbook.SaveCopyAs "\\MyPath\" & "SR50_SN_" &
Range("d3")
& "_" & Format(Now, "yyyymmmdd") & ".xls"
Else
If MsgBox("Are you sure the serial number doesn't begin with
C?", vbYesNo) = vbYes Then
ActiveWorkbook.SaveCopyAs "\\MyPath\" & "SR50_SN_" &
Range("d3")
& "_" & Format(Now, "yyyymmmdd") & ".xls"
Else
MsgBox ("Please fix the serial number.")
End If
End If
End If
End Sub

Thanks for any and all assistance.

Chris