Activating Open Workbooks using VBA
thank you for your assistance, but I am still having issues and I think a
little more information may assist.
The problem with using the workbook.add statement is that the files already
exist and the .add command creates them doesn't it?
The files are created by exporting information via a 'distributed solution'
from Dynamics GP (accounting program) and each export uses a different
template located on a server (shared directory but unmapped). The macro I am
trying to write will be part of the document created by the second export but
needs to grab the data from the first export and append. The first export
does not need to be saved and the second export will be saved or closed
without saving depending on the user.
It is almost like the exports create files that are two separate instances
of Excel and one cannot see the other. Using Workbooks('index') doesn't work
nor does trying .Activate (subscript out of bounds) with or without the file
extension.
It appears that I may have to save, close and re-open each file before
manipulating the data to get it to work. This just seems so cumbersome.
Any suggestions that may overcome this problem would be appreciated. I
really don't want to do the convoluted 'save/close/open' approach if at all
possible.
Thanks
"Dave Peterson" wrote:
Don't rely on the name of the new workbook. Set a variable that represents
those workbooks when you create them.
dim wkbk1 as workbook
set wkbk1 = workbooks.add(template:="c:\yourtemplate.xlt")
'later...
wkbk1.activate
Remember that you don't have to activate/select most things to work on them.
wkbk1.activate
worksheets(1).select
range("A1").select
selection.value = "hi there"
could be replaced with:
wkbk1.worksheets(1).range("A1").value = "hi there"
JC wrote:
Do the workbooks have to be saved before you can activate a workbook using
the Workbooks("abc.xls").Activate command? I can find nothing that suggests
that they need to be. I have two workbooks created from a template that at
the time of running the macro will be unsaved. When attempting to use the
Workbooks.Activate command I get a subscript out of range error which I am
assuming relates to the fact that they are not saved files, however they are
open.
Any assistance to a learner would be greatly appreciated.
--
Dave Peterson
|