VBA Compile error, how to solve?

Janb

New Member
Joined
Feb 19, 2009
Messages
39
Excel 2016 / file is on an external drive (NAS) / using Option Explicit

Hi all,

Yesterday I wrote a (imo) simple vba script and it was working. Another VBA script with the same DIM and Set to my surprise didn't work.

Today none of them are working. The error is: Compile error: Expected variable or procedure, not module.

First the one who worked perfectly yesterday but not anymore

HTML:
Sub WorkbookTasks()

Dim WK As Workbook
Dim WS As Worksheet

'"Y:\001 Office\01 Excel\Zelfstudie\VBA\# Eigen Oefeningen\Workbooks ExcelMacroMastery.xlsm")

Set WK = Workbooks("Workbooks ExcelMacroMastery.xlsm")


    For Each WK In Application.Workbooks
        For Each WS In WK.Worksheets
    
    Debug.Print WK.Name, WS.Name 'Just the name of the file
    
    Debug.Print WK.FullName, WS.Name 'Including the directory
    
        Next WS
            Next WK
End Sub

The one who didn't work yesterday and today (same DIM and Set):

HTML:
Sub WorkbookSave()

Dim WK As Workbook

Set WK = Workbooks("Workbooks ExcelMacroMastery.xlsm")

WK.Save


End Sub

Thanks in advance for your help.

Cheers Jan
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Strange issue. I just wrote the same scripts in a new workbook with a new name and they are all working. Still don't understand the problem in the other workbook (Workbooks ExcelMacroMastery.xlsm)

Code:
Sub PrintWBnames()

Dim WK As Workbook
Dim WS As Worksheet

Set WK = Workbooks("Workbooks Test Macros.xlsm")

For Each WK In Workbooks
    For Each WS In WK.Worksheets

Debug.Print WK.Name, WS.Name

    Next WS
        Next WK
        

End Sub
 
Upvote 0
Do you have a module in your project that has the name : Workbooks ? If so, then change its name
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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