Getting a compile error with a very simple VBA code

borski88

Board Regular
Joined
Jul 3, 2015
Messages
71
I have this simple code open a hidden sheet:

Code:
Sub Test()
    
    Sheets("sheet1").Visible = True
    Sheets("sheet1").Select

End Sub

I get the following Compile Error:
Compile Error:
Wrong number of arguments or invalid property assignments
 
Actually I have a question... are you simply trying to run this code by itself, or are you calling it from somewhere else? If you step through your code does it work?


I was running the code with a button that had the macro binded to it.
 
Last edited:
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
It seems to me that both Sheets("Sheet1") and Application.Sheets("Sheet1") both refer to Sheet1 of the active workbook. So I don't see how it would make a difference. Maybe I'm missing something?
 
Upvote 0
It seems to me that both Sheets("Sheet1") and Application.Sheets("Sheet1") both refer to Sheet1 of the active workbook. So I don't see how it would make a difference. Maybe I'm missing something?


I'm not sure either, but it seemed to work
 
Upvote 0
It seems to me that both Sheets("Sheet1") and Application.Sheets("Sheet1") both refer to Sheet1 of the active workbook. So I don't see how it would make a difference. Maybe I'm missing something?

Yes, they both refer to the same thing. I found the Application.Sheets solution from a similar thread from Googling. I would encourage OP to ensure that they are using the proper references in VBA. Tools>References, make sure you have the correct Object Libraries chosen. I have not found exactly why this works or why it is needed for this issue so my best theory is that he did not have the Microsoft Office 16.0 Object Library selected... not sure, though.
 
Upvote 0
It seems to me that both Sheets("Sheet1") and Application.Sheets("Sheet1") both refer to Sheet1 of the active workbook. So I don't see how it would make a difference. Maybe I'm missing something?
If the OP has a macro or function (maybe class, but I'm not sure about that one) named Sheets, then not using Application will generate the error he reported as VB will attempt to use the macro or function directly... using Application makes the Sheets property reference the workbook's Sheet and not the macro or function with that name. One could use ThisWorkbook instead of Application and it would have the same effect. My advice to the OP is to not use reserved keywords as macro or function names.
 
Last edited:
Upvote 0
@Rick

That explains it. Thanks, I really appreciate it.

@Svendiamond

Thanks for your help as well.
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,665
Members
449,045
Latest member
Marcus05

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