VBA: Compile error: Expected end sub

glhfgtd

New Member
Joined
Aug 2, 2011
Messages
10
i'm trying to empty "ThisWorkbook" module for all excel files in the same directory. Here is my code, but it pops up an Compile error: Expected end sub when i run it.
Please help!

Code:
Sub RemoveWbkCode()
Dim itm As String
    itm = Dir(ThisWorkbook.Path & "\*.xls")
    Do While itm <> ""
        If itm = ThisWorkbook.Name Then GoTo 66
        Workbooks.Open ThisWorkbook.Path & "\" & itm
        With ActiveWorkbook.VBProject.VBComponents("thisworkbook").CodeModule
            .DeleteLines 1, .CountOfLines
        End With
        ActiveWorkbook.Close True
66      itm = Dir
    Loop
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
66 itm = Dir
probably should be
66: itm = Dir

Or, recoded without GoTo:
Code:
Sub RemoveWbkCode()
Dim itm As String
    itm = Dir(ThisWorkbook.Path & "\*.xls")
    Do While itm <> ""
        If itm <> ThisWorkbook.Name Then
	    Workbooks.Open ThisWorkbook.Path & "\" & itm
            With ActiveWorkbook.VBProject.VBComponents("thisworkbook").CodeModule
                .DeleteLines 1, .CountOfLines
            End With
            ActiveWorkbook.Close True
        End If
        itm = Dir
    Loop
End Sub

This looks like dangerous code ... be careful!
 
Upvote 0
Welcome to MrExcel.

It should be 66: instead of 66

However, do not use this kind of structures. By rewriting the if with an inequality (<>) you avoid spaghetti type coding.
 
Upvote 0
^^ I was ready to believe you ... I've seen Dir() just about all the time I think...
 
Upvote 0
thank you Xenou and wigi for the prompt response.
I tried
Xenou's code but it results the same compile error.
then i F8 each line of code to see where it causes the error and found out that the target excel contains Workbook_open, Workbook_BeforeClose and
Workbook_beforesave events, it seems they are causes of conflict.
Would be possible to clear ThisWorkbook module without open the target workbooks? or is there any other way to clean them?
thanks a lot!

 
Last edited:
Upvote 0
or is there any other way to clean them?

I never tried it, but if you save them as .xlsx files, it might be possible. I do not know whether there will be messages from Excel because you save a workbook with macros as a workbook without.
 
Upvote 0
I'm afraid I really don't know. I've never been in such a situation. Many things VBE-related are covered at Chip Pearson's site. It's not entirely clear why this should cause a failure ... why do you need to clear vba from all excel files in a folder?
 
Upvote 0
as part of my work required process. I have to add vba to 30 plus excel files and remove all the codes before achieve them :eeek:
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top