+ Reply to Thread
Results 1 to 6 of 6

VBA to delete Sub or Private Sub in ThisWorkbook from a Module

  1. #1
    Forum Contributor
    Join Date
    08-12-2019
    Location
    NYC
    MS-Off Ver
    MS 365
    Posts
    167

    VBA to delete Sub or Private Sub in ThisWorkbook from a Module

    Hi everyone,

    I would like to create a sub in a new Module that, when run, will delete another particular Sub or Private Sub located in ThisWorkbook, is that doable? Ideally I can refer to the sub I want to delete by its name, rather than play with lines # in the code (because its position in the code and its size (# of lines) will evolve with time).

    Thanks

  2. #2
    Valued Forum Contributor MikeVol's Avatar
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    MSO Prof Plus 2021 x64 (En)
    Posts
    493

    Re: VBA to delete Sub or Private Sub in ThisWorkbook from a Module

    Hello @MagnusNovak. A similar issue was resolved here. If you only need to remove the code from the ThisWorkbook module, then here is the option for you.
    Please Login or Register  to view this content.
    Good luck.
    NOTE: As the original poster/owner, only you can mark your thread as SOLVED (Thread Tools above Post #1).
    You can say "Thanks" in your thread to everyone who offered to help you.
    You can also reward them by clicking * "Add Reputation" under their username on the left.
    With Regards, MikeVol.

  3. #3
    Forum Contributor
    Join Date
    08-12-2019
    Location
    NYC
    MS-Off Ver
    MS 365
    Posts
    167

    Re: VBA to delete Sub or Private Sub in ThisWorkbook from a Module

    Thanks MikeVol. Just to understand better, this code deletes all of the code from ThisWorkbook, not just a particular module, right? My ThisWorkbook includes a lot more code than what I'm trying to delete so I need to target a particular section / sub. More particularly, I'm trying to remove the Private Sub Workbook_SheetSelectionChange, but nothing else outside of that.

  4. #4
    Valued Forum Contributor MikeVol's Avatar
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    MSO Prof Plus 2021 x64 (En)
    Posts
    493

    Re: VBA to delete Sub or Private Sub in ThisWorkbook from a Module

    Hello @MagnusNovak. Yes,
    this code deletes all of the code from ThisWorkbook
    According to Chip Pearson's site. We can find the procedure we need. The code was taken from there and modified to suit your needs.
    Please Login or Register  to view this content.
    Objects In The VBA Extensibility Model

    The following is a list of the more common objects that are used in the VBA Extensibilty object model. This is not a comprehensive list, but will be sufficient for the tasks at hand.

    VBIDE
    The VBIDE is the object library that defines all the objects and values that make up VBProject and the Visual Basic Editor. You must reference this library to use the VBA Extensibility objects. To add this reference, open the VBA editor, open your VBProject in the editor, and go to the Tools menu. There, choose References . In the References dialog, scroll down to Microsoft Visual Basic for Applications Extensibility 5.3 and check that item in the list.
    Good luck.
    Last edited by MikeVol; 10-25-2023 at 05:48 AM. Reason: Update

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,539

    Re: VBA to delete Sub or Private Sub in ThisWorkbook from a Module

    Removing code from a module can fail for two reasons:
    1. If you pass the file to another person then there is a good chance that "Trusted access to VBA project" is NOT checked (by default it is not).
    2. If the project is password protected.

    In my opinion, a better solution would be to create a condition at the beginning of the Workbook_SheetSelectionChange procedure. When a certain condition is met (you didn't specify at what point you would like this event not to occur, so it's hard to answer more precisely) there will be an early exit from the procedure.

    Artik

  6. #6
    Forum Contributor
    Join Date
    08-12-2019
    Location
    NYC
    MS-Off Ver
    MS 365
    Posts
    167

    Re: VBA to delete Sub or Private Sub in ThisWorkbook from a Module

    Thanks a lot MikeVol, I'm far from understanding this code but it does work like a charm!

    Artik, thanks for this precisions. To give some additional background, I'm trying to build a macro that "clean" my spreadsheet before release (so it clears some data in sheets, delete some temporary sheets and delete some VBA modules and functions only used during development). Once that macro is run, it auto-deletes itself and the spreadsheet is "clean" and ready for release. So it's really just a way for me to automatize a few tasks I used to do manually before every release. With MikeVol's code above, I'm now able to clear the last type of content I was previously missing (cells, sheets, modules, VBA code) so that's great!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. ThisWorkbook Private Sub, two or more subrutines?
    By B.W.B. in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 03-05-2019, 11:47 AM
  2. [SOLVED] Using Application.Run to call a private sub in a module from ThisWorkbook object
    By Stormin' in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-24-2017, 10:47 AM
  3. [SOLVED] Calling a private procedure from ThisWorkbook
    By JimDandy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-28-2016, 02:42 AM
  4. [SOLVED] Can't reference ThisWorkbook from a module in another workbook module
    By Bukovnik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2015, 11:32 AM
  5. [SOLVED] Problem with ThisWorkbook Module: Two Private Sub Workbook_Open()
    By DavidRoger in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-22-2015, 11:28 AM
  6. Replies: 1
    Last Post: 08-30-2011, 02:23 AM
  7. [SOLVED] Calls from sheet module to ThisWorkbook module
    By quartz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2005, 11:05 AM

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.6.0 RC 1