You lost me when you said:
"planes orthogonal to the xy-plane"
but that's ok..the environment here is more relevant to me.
Worksheets.Add
ActiveSheet.Name = sName(q)
That snippet checks the Add-In Sub and, if the worksheets don't exist in
it, adds them to the Add-In Sub. I'm using xl2002. The result is the same
if I substitute ThisWorkbook for ActiveWorkbook.
But if I omit ActiveWorkbook in the above snippet, it checks for the
existence of the sheets in the workbook of the calling Sub, i.e., test23D,
and if they don't exist there, it adds them to test23D, which is the
desired result.
Wow. For me
Worksheets.Add
or
ActiveWorkbook.Worksheets.Add
add new sheet(s) to the active workbook, and never to the Addin.xla,
assuming the addin has the IsAddin property set to True or is hidden.
ThisWorkbook.Worksheets.Add adds the sheets only to the Addin.xla. Just to
confirm, you have different results?
Here is how I imagine it. Am I missing something about your configuration?
Sub WksAddTest()
Dim ai As Workbook
Set ai = Workbooks.Add(1)
ai.IsAddin = True
ai.SaveAs "C:\FriTest.xla"
Dim wkb As Workbook
Set wkb = Workbooks.Add(1)
wkb.SaveAs "C:\test23D_999.xls"
Worksheets.Add ' Adds to test23D_999.xls
ActiveWorkbook.Worksheets.Add ' Adds to test23D_999.xls
ThisWorkbook.Worksheets.Add ' Adds to the WksAddTest() workbook
End Sub
Granted your code is in the actual addin, but I get similar results while
trying this in an addin or a regular workbook.
As for the rest, I better understand what you are trying to do. Your first
post and your first reply to me left open in my interpretation the
possibility that you were in the process of creating a multi workbook
solution and could add a parameter, while this detail makes it much clearer
to me (except for the orthogonal part) that your existing structure is
better left unmodified.
However, I have corrected macros such as what follows, well not exactly like
it, but similar in approach, which is what prompted me to go on about my
fully qualified sheet/explicit book reference diatribe.
Basically, the macro emulates several steps that would be performed in
different pieces, manually or programmatically, and ends up deleting the
important workbook rather than the disposable one. Call me a defensive
developer, but stuff like this exists out there (!).
Sub WorstCasePossibility()
Dim wkb As Workbook
' Create a temporary workbook
Set wkb = Workbooks.Add(1)
wkb.SaveAs "C:\MyTempWkb.xls"
' Somehow, this gets set, either
' by a macro or advanced user...
ActiveWindow.Visible = False
' ...and changes are saved
wkb.Save
wkb.Close False
Set wkb = Nothing
' Now let's create my big important
' workbook and save it
Set wkb = Workbooks.Add(1)
wkb.Worksheets(1).Name = "My real important data."
wkb.SaveAs "C:\MyImportantWkb.xls"
Set wkb = Nothing
' Months go by, and my real important workbook
' now has all of my important data in it
' Let's open the temp workbook and do some
' additional data import into my important workbook
Workbooks.Open "C:\MyTempWkb.xls"
' Now my temp workbook is open, so
' programmatically import data from it into
' my big important workbook
' The macro is done with the temp.xls, so let's get rid of it
' Let's just use the ActiveWorkbook because we just
' opened it, so we know which one it is
ActiveWorkbook.ChangeFileAccess (xlReadOnly)
Kill ActiveWorkbook.FullName
ActiveWorkbook.Close False
For Each wkb In Application.Workbooks
Debug.Print wkb.Name
Next
End Sub