I would like to delete all the empty worksheet modules for the active workbook instead of removing these one by one
it would be appreciated if someone could assist me
I would like to delete all the empty worksheet modules for the active workbook instead of removing these one by one
it would be appreciated if someone could assist me
As a worksheet module can only be deleted by deleting the worksheet itself …
Thanks Marc
module1 is the main module, but by using the macro recorder one could end up with several modules that one no longer requires but one needs to delete these manually one by one
This routine will remove Module1 from the current project.
This code should not be in Module1
![]()
Sub test() Dim modName As String, xMod As VBComponent Dim strPrompt As String modName = "Module1" Set xMod = ThisWorkbook.VBProject.VBComponents(modName) With xMod.CodeModule strPrompt = .Lines(1, .CountOfLines) End With strPrompt = "Remove " & xMod.Name & vbCr & vbCr & strPrompt If MsgBox(strPrompt, vbYesNo) = vbYes Then ThisWorkbook.VBProject.VBComponents.Remove xMod End If End Sub
_
...How to Cross-post politely...
..Wrap code by selecting the code and clicking the # or read this. Thank you.
Thanks mickerickson for the code
I need to remove all blank modules for the activeworkbook, except module1
In the previous post 'Module1' can't be obviously a worksheet module …
I was just giving the syntax for removing a code module.
The loop to check code modules is straight-forward.
As you write that loop, you need to test if your editor has automatically inserted Option Explicit.
thanks guys
The modules appear as
This works - but it's set to delete a module with less than three lines - take care
![]()
Sub CountLinesinModule(): Dim wba As Workbook: Set wba = ActiveWorkbook Dim M As Object, N As String, X As Integer, C As Object For Each M In wba.VBProject.VBComponents: N = M.Name If N Like "Sheet*" Then GoTo GetNext If InStr(1, N, "ThisWorkbook") Then GoTo GetNext Set C = wba.VBProject.VBComponents(N).CodeModule: X = C.CountOfLines If X < 3 Then wba.VBProject.VBComponents.Remove M GetNext: Next End Sub
If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)
You can't do one thing. XLAdept
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin
Hi xladept
Thanks , your code works perfectly
You're welcome and thanks for the rep!![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks