mattboy
mattboy

Reputation: 2910

Checking if two worksheets are the same

I'm trying to check if one worksheet is the same as another one. The example below shows how I'm doing it.

Dim sheet1 as Worksheet, sheet2 as Worksheet
Set sheet1 = GetObject("C:\sheet1.xls").Worksheets(1)
Set sheet2 = GetObject("C:\sheet2.xls").Worksheets(1)

If sheet1 = sheet2 Then Debug.Print("Same workbook") 'Should of course return false, but instead gives an error

But this gives the error Object doesn't support this property or method. So how does one go about comparing two sheets like this?

Upvotes: 0

Views: 2149

Answers (2)

CarloC
CarloC

Reputation: 155

Use Is...

If sheet1 Is sheet2 Then Debug.Print("Same workbook")

Details are in this msdn article (except IsNot doesn't seem to be supported in VBA).

Upvotes: 2

Helio Santos
Helio Santos

Reputation: 6815

If you have defined timesheet names you can compare the name property.

If sheet1.Name = sheet2.Name Then Debug.Print("Same workbook")

Upvotes: 1

Related Questions