Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default Workbooks Array

Excel XP & Win XP
I want to work with a group of workbooks, one at a time. I wanted to use
the following statement but I get a "Type Mismatch" error on the "For
Each..." line. All the workbooks are open.
My code, simplified, is:
Dim wbOther as Workbook
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
MsgBox wbOther.Name
Next wbOther
How can I setup a "For" loop for a group of workbooks?
Thanks for your time. Otto


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Workbooks Array

dim i As long
dim arr

arr = Array("One", "Two", "Three")

for i = 0 to ubound(arr)
msgbox workbooks(arr(i)).Name
next i


RBS


"Otto Moehrbach" wrote in message
...
Excel XP & Win XP
I want to work with a group of workbooks, one at a time. I wanted to use
the following statement but I get a "Type Mismatch" error on the "For
Each..." line. All the workbooks are open.
My code, simplified, is:
Dim wbOther as Workbook
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
MsgBox wbOther.Name
Next wbOther
How can I setup a "For" loop for a group of workbooks?
Thanks for your time. Otto


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Workbooks Array

Use a collection and not an array... Something like this...

Sub test()
Dim col As Collection
Dim wbk As Workbook

Set col = New Collection
col.Add Workbooks("Book2"), "Book2"
col.Add Workbooks("Book3"), "Book3"
col.Add Workbooks("Book4"), "Book4"

For Each wbk In col
MsgBox wbk.Name
Next wbk

End Sub
--
HTH...

Jim Thomlinson


"Otto Moehrbach" wrote:

Excel XP & Win XP
I want to work with a group of workbooks, one at a time. I wanted to use
the following statement but I get a "Type Mismatch" error on the "For
Each..." line. All the workbooks are open.
My code, simplified, is:
Dim wbOther as Workbook
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
MsgBox wbOther.Name
Next wbOther
How can I setup a "For" loop for a group of workbooks?
Thanks for your time. Otto



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Workbooks Array

Don't think that approach will work, but you could try

For Each wb OtherIn Workbooks

If Not IsError(Application.Match(wb.Name, (Array("One.xls",
"Two.xls", "Three.xks")), 0)) Then

MsgBox wbOther.Name
End If
Next wb


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Otto Moehrbach" wrote in message
...
Excel XP & Win XP
I want to work with a group of workbooks, one at a time. I wanted to use
the following statement but I get a "Type Mismatch" error on the "For
Each..." line. All the workbooks are open.
My code, simplified, is:
Dim wbOther as Workbook
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
MsgBox wbOther.Name
Next wbOther
How can I setup a "For" loop for a group of workbooks?
Thanks for your time. Otto



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Workbooks Array

Otto Moehrbach wrote:
Excel XP & Win XP
I want to work with a group of workbooks, one at a time. I wanted to use
the following statement but I get a "Type Mismatch" error on the "For
Each..." line. All the workbooks are open.
My code, simplified, is:
Dim wbOther as Workbook
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
MsgBox wbOther.Name
Next wbOther
How can I setup a "For" loop for a group of workbooks?
Thanks for your time. Otto


The following works:

Sub test12()
Dim x, y As Byte
x = Array("One.xls", "Two.xls", "Three.xls")
For y = LBound(x) To UBound(x)
Debug.Print Workbooks(x(y)).Name
Next y
End Sub

Alan Beban


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default Workbooks Array

Thanks guys. I see that I can't set up an array of workbooks so I went with
the array of workbook names. I just thought there was a better way.
Thanks again. Otto
"Otto Moehrbach" wrote in message
...
Excel XP & Win XP
I want to work with a group of workbooks, one at a time. I wanted to use
the following statement but I get a "Type Mismatch" error on the "For
Each..." line. All the workbooks are open.
My code, simplified, is:
Dim wbOther as Workbook
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
MsgBox wbOther.Name
Next wbOther
How can I setup a "For" loop for a group of workbooks?
Thanks for your time. Otto



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Workbooks Array

I see that I can't set up an array of workbooks

You can set up an array of workbooks:

Sub test()

Dim i As Long
Dim arrWBs(1 To 2) As Workbook

Set arrWBs(1) = Workbooks("Book1")
Set arrWBs(2) = Workbooks("Book2")

For i = 1 To 2
MsgBox arrWBs(i).Name
Next

End Sub

Note that if the workbook hasn' t been saved yet you leave
the extension off, but if the workbook was saved then you add the .xla or
..xla.


RBS

"Otto Moehrbach" wrote in message
...
Thanks guys. I see that I can't set up an array of workbooks so I went
with the array of workbook names. I just thought there was a better way.
Thanks again. Otto
"Otto Moehrbach" wrote in message
...
Excel XP & Win XP
I want to work with a group of workbooks, one at a time. I wanted to use
the following statement but I get a "Type Mismatch" error on the "For
Each..." line. All the workbooks are open.
My code, simplified, is:
Dim wbOther as Workbook
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
MsgBox wbOther.Name
Next wbOther
How can I setup a "For" loop for a group of workbooks?
Thanks for your time. Otto




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default Workbooks Array

RB
Of course you are correct. I wasn't clear when I said that I can't
setup an array of workbooks. What I meant was that I couldn't setup an
array of workbooks in one line similar to what was in my original post:
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
Otto
"RB Smissaert" wrote in message
...
I see that I can't set up an array of workbooks


You can set up an array of workbooks:

Sub test()

Dim i As Long
Dim arrWBs(1 To 2) As Workbook

Set arrWBs(1) = Workbooks("Book1")
Set arrWBs(2) = Workbooks("Book2")

For i = 1 To 2
MsgBox arrWBs(i).Name
Next

End Sub

Note that if the workbook hasn' t been saved yet you leave
the extension off, but if the workbook was saved then you add the .xla or
.xla.


RBS

"Otto Moehrbach" wrote in message
...
Thanks guys. I see that I can't set up an array of workbooks so I went
with the array of workbook names. I just thought there was a better
way. Thanks again. Otto
"Otto Moehrbach" wrote in message
...
Excel XP & Win XP
I want to work with a group of workbooks, one at a time. I wanted to
use the following statement but I get a "Type Mismatch" error on the
"For Each..." line. All the workbooks are open.
My code, simplified, is:
Dim wbOther as Workbook
For Each wbOther In Workbooks(Array("One.xls", "Two.xls", "Three.xls"))
MsgBox wbOther.Name
Next wbOther
How can I setup a "For" loop for a group of workbooks?
Thanks for your time. Otto






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create an array of all open workbooks JNW Excel Programming 2 August 24th 06 04:29 PM
Opening Workbooks / Filling Array Bill Excel Programming 1 January 19th 06 08:19 PM
SOS-How to pass array parameter to Workbooks.OpenText(...) in VC++ Lily Excel Programming 1 September 27th 05 02:05 PM
Array Sheets Copy to new Workbooks Witek Kruk Excel Programming 2 October 18th 04 01:11 AM
Use Array to activate workbooks Stuart[_5_] Excel Programming 3 December 2nd 03 08:17 PM


All times are GMT +1. The time now is 07:11 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"