View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 145
Default Addin vs. Embedded references

"ThisWorkbook" is not equivalent to "ActiveWorkbook" when running code from
an xla.
ThisWorkbook refers to the xla itself.

Always good practice to explicitly qualify all references to worksheets by
including a workbook reference.

ie. don't use just
Sheets("Formulas").Activate
if you have a specific workbook in mind.


Tim

"Kigol" wrote in message
ups.com...
Simple question, complicated answer. Why when I run this code from an
add-in does it not clear anything and error on the last line with
"Subscript out of Range"? It's vague I know but it doesn't make any
sense to me.
The code works completely fine when ran separately from the macros
which are part of my addin.


Dim sh As Worksheet
wks = ActiveSheet.Name
For Each sh In ThisWorkbook.Sheets
sh.Activate
If Not sh.Name = "Formulas" Then
Range(Rows(1), Rows(65536)).Clear
ActiveSheet.DrawingObjects.Delete
End If
Next
Sheets("Formulas").Activate