+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Delete VB Codes in new workbook

  1. #1
    Registered User
    Join Date
    09-06-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Delete VB Codes in new workbook

    Hi,

    I have a button on a worksheet when clicked, will copy the current ActiveSheet to a new workbook and allow the user to save the new workbook elsewhere.

    but the new workbook still have the codes in them, which (I think) led to the security warning on macros when opening it.

    Would like to find out if I can remove the codes in the new workbook so that user would not encounter the security warning for macros?

    TIA
    jwswks

  2. #2
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    825

    Re: Delete VB Codes in new workbook

    hi,
    I use something similar...see if it helps you
    Sub Button5_Click()
    sFilename = sPath & ActiveSheet.Name & ".xlsx"
    
    'copy the active sheet into new workbook
    ActiveSheet.Copy
    ActiveWorkbook.SaveAs _
    Filename:=sFilename, _
    FileFormat:=51, _
    Password:="", _
    CreateBackup:=False
    ActiveSheet.Shapes("Button 5").Delete
        ActiveSheet.Shapes("Button 3").Delete
    ActiveSheet.Shapes("CommandButton1").Delete
    ActiveSheet.Shapes("Button 2").Delete
        ActiveSheet.Shapes("Button 1").Delete
    End Sub
    Last edited by john55; 09-06-2011 at 09:31 AM. Reason: cor
    Regards, John

  3. #3
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Delete VB Codes in new workbook

    Is the code in the worksheet module?
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  4. #4
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    825

    Re: Delete VB Codes in new workbook

    I am sorry, I forgot to mention that is attached to a button.
    Regards, John

  5. #5
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Delete VB Codes in new workbook

    But is the code in the sheet module? If you don't know then attach the workbook
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  6. #6
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    825

    Re: Delete VB Codes in new workbook

    no, is not in a sheet module.
    Attached Files Attached Files
    Regards, John

  7. #7
    Registered User
    Join Date
    09-06-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Delete VB Codes in new workbook

    Hi John,

    Thanks for the codes.

    As royUK mentioned I attached my codes to the sheet module.

    Is there any way to delete them?

    Thanks
    jwswks
    Last edited by jwswks; 09-07-2011 at 04:56 AM.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Delete VB Codes in new workbook

    Hello jwswks,

    This code revision should work for you.
    Sub Button5_Click()
    
      Dim VBcomp As Object
      Dim VBproj As Object
      Dim Wkb As Workbook
      
        sFilename = sPath & ActiveSheet.Name & ".xlsx"
    
        'copy the active sheet into new workbook
         ActiveSheet.Copy
         ActiveWorkbook.SaveAs _
         Filename:=sFilename, _
         FileFormat:=51, _
         Password:="", _
         CreateBackup:=False
         
         ActiveSheet.Shapes("Button 5").Delete
         ActiveSheet.Shapes("Button 3").Delete
         ActiveSheet.Shapes("CommandButton1").Delete
         ActiveSheet.Shapes("Button 2").Delete
         ActiveSheet.Shapes("Button 1").Delete
        
       ' Set the workbook variable to point to the new workbook
         Set Wkb = ActiveWorkbook
        
       ' Access the VBProject for the new workbook
         Set VBproj = Wkb.VBProject
         
       ' Get the code module for the active sheet of the new workbook
         Set VBcomp = VBproj.VBComponents(ActiveSheet.Name)
        
       ' Delete all the macro code
         With VBcomp.CodeModule
           .DeleteLines 1, .CountOfLines
         End With
         
       ' Save the changes
         Wkb.Save SaveChanges:=True
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  9. #9
    Registered User
    Join Date
    09-06-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Delete VB Codes in new workbook

    Thanks!!

    I will give it a try :D

    jwswks
    Last edited by jwswks; 09-07-2011 at 04:57 AM.

  10. #10
    Registered User
    Join Date
    09-06-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Delete VB Codes in new workbook

    Hi,

    I have tried to insert your code to the sheet but there is a
    "Subscript out of range error in the highlight line."

    Is it due to me coding in excel 2003
    Or did I miss out any references objects I needa add?

    Private Sub cmdSave_Click()
        Dim nwb As Workbook
        Dim nws As Worksheet
        Dim VBproj As Object
        Dim VBObj As Object
        Dim DstFile As String
              
        Sheets("Records").Copy
        
        Set nwb = ActiveWorkbook
        Set nws = ActiveSheet
        Set VBproj = ActiveWorkbook.VBProject
            
        ActiveSheet.Name = "Abcd"
        Set VBObj = VBproj.VBComponents("Abcd")
        ActiveSheet.OLEObjects("cmdSave").Delete
        ActiveSheet.OLEObjects("cmdPrint").Delete
        ActiveSheet.Cells(1, 1).EntireRow.Delete
                   
        'Prompt for SaveAs name
        DstFile = Application.GetSaveAsFilename _
        (InitialFileName:="abcd.xls", _
        Title:="Save As")
        If DstFile = "False" Then
            MsgBox "File not Saved, Actions Cancelled."
            ActiveWorkbook.Close
            Exit Sub
        Else
            ActiveWorkbook.SaveAs DstFile 'Save file
            
            ' Delete all the macro code
            With VBObj.CodeModule
                .DeleteLines 1, .CountOfLines
            End With
         
            ' Save the changes
            nwb.Save SaveChanges:=True
            ActiveSheet.Select 'Set Focus on new file
        End If
    
        
    End Sub

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Delete VB Codes in new workbook

    Hello jwswks,

    I am confused by this post as it is different from the previous one. Perhaps if you explain what you are doing, I can write a macro to accommodate your needs.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  12. #12
    Registered User
    Join Date
    09-06-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Delete VB Codes in new workbook

    Hi Leith,

    Sorry that I confuse you.

    I actually have a excel sheet containing customer information.

    The purpose of the program is to let user to:
    1) Search for records according to 1 or more filter keywords that is selected by the user.
    2) Display the searched records on "Records" sheet
    3) Have a button on "Records" sheet to allow user to save them in a new workbook in the event they need to pass the information around.

    Im not actually using macros(I think thats what confusing?) but rather using just VB editor to code the whole project.

    I have completed 1 & 2 and the code above is for 3.

    The above code is written in "Records" worksheet

    After a copy of the worksheet (which appear as a new workbook), I deleted the buttons and the 1st row in the new workbook so that it would appear like a normal excel file.(As I place the button on the 1st row of "Records" Sheet)

    Just that the codes in the sheet get copied as well and when I tried to open it again, the macros security prompt shows up.

    So now I am trying to delete the codes in the new workbook, to make it look like a normal excel file to prevent the prompt from showing up.

    Sorry again if I confuse you as sometimes my brain starts running around when I try to think things through

    PS: Flow would be like

    1) User select keywords
    2) Display records on "Records" Sheet
    3) User click save button on "Records" Sheet
    4) Sheet copied to new workbook
    5) User save the new workbook
    Last edited by jwswks; 09-07-2011 at 04:55 AM.

  13. #13
    Registered User
    Join Date
    09-06-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Delete VB Codes in new workbook

    Hi Guys,

    After searching the net for some solutions, managed to get it to work by declaring it as

    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    But the prompt for the new workbook still appears though
    Last edited by royUK; 09-07-2011 at 07:51 AM.

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Delete VB Codes in new workbook

    Hello jwswks,

    If the prompt is still appearing Then not all of the modules are clear of text. Check each sheet module and this workbook of the copied workbook and look for any text in them. Even an empty Sub procedure will still trigger the macro warning.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  15. #15
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Delete VB Codes in new workbook

    You are using macros.

    Do you intend protecting the VBA project/ If you do then the code to delete the code won't work.

    I would simply copy the results to Word or a pdf
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0