Results 1 to 5 of 5

Thread: [RESOLVED] Delete code in workbooks using a macro

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Resolved [RESOLVED] Delete code in workbooks using a macro

    I am trying to write a macro that will delete the code behind a particular worksheet. I tried the following code but I keep getting a compile error after it deletes the lines.

    Code Code:
    1. Dim VBProj As VBIDE.VBProject
    2. Dim VBComp As VBIDE.VBComponent
    3.  
    4. Set VBProj = ActiveWorkbook.VBProject
    5. Set CodeMod = ActiveWorkbook.VBProject.VBComponents("sheet1").CodeModule
    6.  
    7. CodeMod.DeleteLines 1, CodeMod.CountOfLines


    First I get the following compile error:

    "Only comments may appear after End Sub, End Function, or End Property"

    I click "OK" and then I get the following run-time error:

    "Method '_Default' of object 'Range' failed."

    Regardless if I hit Debug or End, the program closes.

    Any help is appreciated.
    Chrissy

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Delete code in workbooks using a macro

    Where are you trying to sub this code from?

    It sounds like you are running it from the declarations section rather than from an actual sub routine.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Delete code in workbooks using a macro

    No, it is in a sub within a module...

    vb Code:
    1. Sub DeleteCode()
    2.  
    3. Dim VBProj As VBIDE.VBProject
    4. Dim VBComp As VBIDE.VBComponent
    5. Workbooks("07540.xls").Activate
    6. Set VBProj = Workbooks("07540.xls").VBProject
    7. Set CodeMod = ActiveWorkbook.VBProject.VBComponents("sheet1").CodeModule
    8.  
    9. CodeMod.DeleteLines 1, CodeMod.CountOfLines
    10.  
    11. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Delete code in workbooks using a macro

    The code actually runs, it is AFTER it deletes the lines that it gives the error.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Delete code in workbooks using a macro

    Nevermind... the code works... the problem is on another row because of the deletion. Sorry!

Posting Permissions

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



Click Here to Expand Forum to Full Width