View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
CB CB is offline
external usenet poster
 
Posts: 97
Default Deleting all code in new file only (creating using save as)

Hi Chip,

Thanks for responding. I was perplexed by your statement...

"When you do the Save As operation, Original.xls is closed and SavedAs.xls
is open and the Active Workbook. "

This is not the behaviour I see. When I click the button to save the file, a
new file is saved in the background (in that I don't see it open at all) that
contains all the data that was entered and my original file remains open. Of
course, the original files also contains the data but I have a button that
clears the data so the tech can run another test.

Chris

"Chip Pearson" wrote:


As others have pointed out, the code as written works on the
ActiveWorkbook, specified only on the line

Set VBProj = ActiveWorkbook.VBProject

As I see it, you have two workbooks under consideration:

Original.xls
' and
SavedAs.xls

When you do the Save As operation, Original.xls is closed and
SavedAs.xls is open and the Active Workbook. If you want to remove
all the VBA code from the SavedAs.xls workbook, you can run the code
as is. However, if you need to run the code against Original.xls (or
any other closed workbook), you will need to open that workbook. The
code works only on open workbooks. You can specify the workbook whose
code is to be stripped with something like

Set VBProj = Workbooks("WorkbookName.xls")

If you need to open the workbook, you can use code like

Dim WB As Workbook
Set WB = Workbooks.Open("C:\Book1.xls")
Set VBProj = WB.VBProject

The variable WB is set to the instance of the opened C:\Book1.xls
workbook and the VBProj variable is set to the VBProject of that
workbook. All the rest of the code will refer to that workbook.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Thu, 19 Mar 2009 10:10:01 -0700, CB
wrote:

Hi everyone,

Programming in Excel is rather new to me. Ive 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.

Im 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. Ive 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, Im 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