DaveU
DaveU

Reputation: 1082

Worksheet object declaration

I've always declared a worksheet object using the syntax Dim w as Worksheet, but recently I came across some code that used Dim w as Excel.Worksheet.

I checked with the Object Browser which stated that Worksheet is a member of Excel. So my question is why does simply Dim ws as Worksheet also work?

I suspect it might be that Excel objects do not require the Excel prefix, but so far I've not come across a definitive statement to that effect.

Upvotes: 1

Views: 88

Answers (1)

user11198948
user11198948

Reputation:

In Excel VBA they are for all intents and purposes the same thing. Excel is the 'parent' of worksheet but it is implicitly defined within the Excel VBA workspace. In general it is not required when working within Excel VBA as it is the default.

However,if you were working within Word VBA and had imported the Microsoft Excel x.x object library, you would need to specify Excel.Worksheet since the Word application model would be the default and it does not contain a Worksheet type.

Upvotes: 6

Related Questions