View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
JDaywalt JDaywalt is offline
external usenet poster
 
Posts: 37
Default Execute VB code against certain open workbooks

Thank you for your quick response. I am still having issues. I tried using
your suggestion, but it didn't seem to work and I'm sure it's because I have
something wrong. FYI, the code is executed from within the Master file. It
is supposed to go to "Book1", highlight all cells, then go back to the Master
file & paste the data into a specific sheet tab. It should then return to
"Book1", close that workbook, then proceed to "Book2", etc. until it
completes all files that have "Book" as the prefix.

Sub test()

Dim CAGtemplate
Sheets("Xref").Range("A1").Select
CAGtemplate = ActiveCell.Value

Dim wbk As Workbook

For Each wbk In Workbooks
If Left(wbk.Name, 4) = "Book" Then
Cells.Select
Selection.Copy
Windows(CAGtemplate).Activate
Sheets("Dollars").Select
Range("A1").Select
ActiveSheet.Paste
Windows(wbk).Activate
ActiveWindow.Close
Else
End If

Next wbk
End Sub


"Jim Thomlinson" wrote:

Two possibilities. On is to validate the file name. the other is to validate
the contents of the workbook. If all of your files strictly follow a naming
conventions then:

sub test()
dim wbk as workbook

for each wbk in workbooks
if left(wbk.name, 6) = "MyFile" then 'or whtever the naming convention
msgbox wbk.name
end if
next wbk
end sub

If the books have not been save or do not follow a stric naming convention
then you need to look for som identifying feature in the workbook. This could
be a very hidden sheet that you add at the time the workbooks are created or
some other bit of info that uniquely identifies the file type.
--
HTH...

Jim Thomlinson


"JDaywalt" wrote:

I should have added this as well --- how do I ensure this code is only
executed against the files that contain the naming convention of Book1,
Book2, etc. In other words, I do not want the code to execute against the
Master file, and I also don't want it to execute against anything like a
Personal.xls file or other type of background file that may open
automatically when the user opens their Excel application.

"Jim Thomlinson" wrote:

Try code something like this...

sub test()
dim wbk as workbook

for each wbk in workbooks
msgbox wbk.name
next wbk
end sub
--
HTH...

Jim Thomlinson


"JDaywalt" wrote:

I have a 'Master' file that contains code in which I need to access other
open files and perform a subset of code against each of them. The possible
number of open files is anywhere from 1 to 12---and each of the files will be
named sequentially Book1, Book2, Book3, etc. as they will have been generated
from a previous 'Copy Sheet' process performed by the user. How can I write
my code such that it identifies the number of these "Books" that are open,
then loops through each of them (performing the addt'l code), then stopping
when it reaches the last open book?