Help with dim coding

Derek

Well-known Member
Joined
Feb 16, 2002
Messages
1,595
Hi Guys

A long used macro has suddenly produced a run-time error on this line:

Dim sht As Worksheet
For Each sht In ThisWorkbook.Sheets

Can anyone tell me why this code is not acceptable?

Many thanks

Derek
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I always use worksheets instead of sheets, can not remember why (maybe a question of versions?). I also never use ThisWorkbook since by default they are all ThisWorkbook (and I often forget to rename them). So I rather use:

1579682230179.png
 
Upvote 0
@Kamolga said "also never use ThisWorkbook since by default they are all ThisWorkbook "

Not in my dreams would I rename ThisWorkbook. To the Module code it simply means "code in this workbook" and when mentioned execute for ThisWorkbook. Changing the name makes portability a potential nightmare. Besides, in testing, renaming TB did not affect the code, it still runs as ThisWorkbook. But teased as I was went to look and someone complained

Renaming Workbook Module
Hi,

I am trying to rename the workbook module from ThisWorkbook to something else through the properties window. I can change the name fine, but when I close the file and reopen it the workbook module name has reverted back to ThisWorkbook. Any ideas?

Thanks
The ideas was replied to

Thisworkbook is a built in reference to the workbook and should not be renamed. Why do you want to rename it? -- Andy Pope, who further expanded why it was a bad idea.

As for Sheets v Worksheets, latter is always a spreadsheet, former can also be the above mentioned Chart sheet, so the a .Sheets is not a good idea if you have that mix. Difference between Sheets and Worksheets in VBA - Excel Off The Grid

But, it's just my opinion, and sticking to it, has as yet never broken my code. Beside Set MyWbMain = ThisWorkbook sure as heck makes the name readable inside code that touches on multiple workbooks.
 
Upvote 0
I also never use ThisWorkbook since by default they are all ThisWorkbook (and I often forget to rename them)

ThisWorkbook is a different thing from ActiveWorkbook. ThisWorkbook always refers to whatever workbook the code is in, whereas ActiveWorkbook refers to whichever workbook happens to be active.
 
Upvote 0
Thank you to everyone who replied.

Strangely the same workbook has this coding in a number of places and has been used daily for years without breaking on this line. However, changing Sheets to Worksheets appears to have fixed it.

The assistance of everyone is greatly appreciated

Derek
 
Upvote 0

Forum statistics

Threads
1,220,929
Messages
6,156,901
Members
451,386
Latest member
Jeaux

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