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

On Jul 6, 5:49 pm, Dave Peterson wrote:
When your code is in the Addin, then ThisWorkbook refers to that addin.

Maybe you wanted:
For Each sh In activeworkbook.WorkSheets
(Notice that I'm looping through the .worksheets, too)

You could do the same without selecting:

Dim sh As Worksheet
For Each sh In activeworkbook.worksheets
if lcase(sh.name) = lcase("Formulas") then
'do nothing
else
.cells.clear
.drawingobjects.delete
end if
next sh
'you may not even need this line--since you didn't change the active sheet.
worksheets("Formulas").activate





Kigol wrote:

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


--

Dave Peterson- Hide quoted text -

- Show quoted text -


That makes sense. This is most likely the cause of all of my reference
errors in this new addin module. Thanks for the help guys. I ended up
with this. Good enough for my purposes.


Dim sh As Worksheet
wks = ActiveWorkbook.ActiveSheet.Name
For Each sh In ActiveWorkbook.Worksheets
sh.Activate
If Not sh.Name = "Formulas" Then
sh.Cells.Clear
ActiveWorkbook.ActiveSheet.DrawingObjects.Delete
End If
Next sh