PC Review


Reply
Thread Tools Rate Thread

Array of workbooks. Can this be done?

 
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      13th Nov 2006
This question really relates to something I want to do in PowerPoint, but I'm
guessing that the same type of thing would apply in Excel as well.

I'd like to identify four workbooks to open programmatically. I'd like to
assign them to oWB1, oWB2, oWB3 and oWB4 as they are opened. Can I refer to
them like this

for i = 1 to 4
'do things with workbook owb & i
next i

Really what I'm doing is opening presentations and identifying them in a
similar way. The presentations are physically located on a server that takes
a while to access, so I don't want to open and close them repeatedly.

Thanks
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      13th Nov 2006
I think I'd use an actual array:

Option Explicit
Sub testme01()

Dim myWBNames As Variant
Dim myWkBks() As Workbook
Dim iCtr As Long

myWBNames = Array("C:\a.xls", "c:\b.xls", "C:\c.xls")

ReDim myWkBks(LBound(myWBNames) To UBound(myWBNames))

For iCtr = LBound(myWkBks) To UBound(myWkBks)
Set myWkBks(iCtr) = Workbooks.Open(Filename:=myWBNames(iCtr))
'do more stuff right after opening
Next iCtr

'or wait until they're all open and do stuff then
For iCtr = LBound(myWkBks) To UBound(myWkBks)
'do more stuff
Next iCtr

End Sub



Barb Reinhardt wrote:
>
> This question really relates to something I want to do in PowerPoint, but I'm
> guessing that the same type of thing would apply in Excel as well.
>
> I'd like to identify four workbooks to open programmatically. I'd like to
> assign them to oWB1, oWB2, oWB3 and oWB4 as they are opened. Can I refer to
> them like this
>
> for i = 1 to 4
> 'do things with workbook owb & i
> next i
>
> Really what I'm doing is opening presentations and identifying them in a
> similar way. The presentations are physically located on a server that takes
> a while to access, so I don't want to open and close them repeatedly.
>
> Thanks


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      13th Nov 2006
And include the index:

For iCtr = LBound(myWkBks) To UBound(myWkBks)
'do more stuff
msgbox mywkbks(ictr).name 'for example
Next iCtr

Dave Peterson wrote:
>
> I think I'd use an actual array:
>
> Option Explicit
> Sub testme01()
>
> Dim myWBNames As Variant
> Dim myWkBks() As Workbook
> Dim iCtr As Long
>
> myWBNames = Array("C:\a.xls", "c:\b.xls", "C:\c.xls")
>
> ReDim myWkBks(LBound(myWBNames) To UBound(myWBNames))
>
> For iCtr = LBound(myWkBks) To UBound(myWkBks)
> Set myWkBks(iCtr) = Workbooks.Open(Filename:=myWBNames(iCtr))
> 'do more stuff right after opening
> Next iCtr
>
> 'or wait until they're all open and do stuff then
> For iCtr = LBound(myWkBks) To UBound(myWkBks)
> 'do more stuff
> Next iCtr
>
> End Sub
>
> Barb Reinhardt wrote:
> >
> > This question really relates to something I want to do in PowerPoint, but I'm
> > guessing that the same type of thing would apply in Excel as well.
> >
> > I'd like to identify four workbooks to open programmatically. I'd like to
> > assign them to oWB1, oWB2, oWB3 and oWB4 as they are opened. Can I refer to
> > them like this
> >
> > for i = 1 to 4
> > 'do things with workbook owb & i
> > next i
> >
> > Really what I'm doing is opening presentations and identifying them in a
> > similar way. The presentations are physically located on a server that takes
> > a while to access, so I don't want to open and close them repeatedly.
> >
> > Thanks

>
> --
>
> Dave Peterson


--

Dave Peterson
 
Reply With Quote
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      13th Nov 2006
Thanks, I'll try this.

"Dave Peterson" wrote:

> And include the index:
>
> For iCtr = LBound(myWkBks) To UBound(myWkBks)
> 'do more stuff
> msgbox mywkbks(ictr).name 'for example
> Next iCtr
>
> Dave Peterson wrote:
> >
> > I think I'd use an actual array:
> >
> > Option Explicit
> > Sub testme01()
> >
> > Dim myWBNames As Variant
> > Dim myWkBks() As Workbook
> > Dim iCtr As Long
> >
> > myWBNames = Array("C:\a.xls", "c:\b.xls", "C:\c.xls")
> >
> > ReDim myWkBks(LBound(myWBNames) To UBound(myWBNames))
> >
> > For iCtr = LBound(myWkBks) To UBound(myWkBks)
> > Set myWkBks(iCtr) = Workbooks.Open(Filename:=myWBNames(iCtr))
> > 'do more stuff right after opening
> > Next iCtr
> >
> > 'or wait until they're all open and do stuff then
> > For iCtr = LBound(myWkBks) To UBound(myWkBks)
> > 'do more stuff
> > Next iCtr
> >
> > End Sub
> >
> > Barb Reinhardt wrote:
> > >
> > > This question really relates to something I want to do in PowerPoint, but I'm
> > > guessing that the same type of thing would apply in Excel as well.
> > >
> > > I'd like to identify four workbooks to open programmatically. I'd like to
> > > assign them to oWB1, oWB2, oWB3 and oWB4 as they are opened. Can I refer to
> > > them like this
> > >
> > > for i = 1 to 4
> > > 'do things with workbook owb & i
> > > next i
> > >
> > > Really what I'm doing is opening presentations and identifying them in a
> > > similar way. The presentations are physically located on a server that takes
> > > a while to access, so I don't want to open and close them repeatedly.
> > >
> > > Thanks

> >
> > --
> >
> > Dave Peterson

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      13th Nov 2006
Dave,

I won't know the workbook names until I do some other things. How do I
dynamically create an array?

Thanks,
Barb

"Dave Peterson" wrote:

> I think I'd use an actual array:
>
> Option Explicit
> Sub testme01()
>
> Dim myWBNames As Variant
> Dim myWkBks() As Workbook
> Dim iCtr As Long
>
> myWBNames = Array("C:\a.xls", "c:\b.xls", "C:\c.xls")
>
> ReDim myWkBks(LBound(myWBNames) To UBound(myWBNames))
>
> For iCtr = LBound(myWkBks) To UBound(myWkBks)
> Set myWkBks(iCtr) = Workbooks.Open(Filename:=myWBNames(iCtr))
> 'do more stuff right after opening
> Next iCtr
>
> 'or wait until they're all open and do stuff then
> For iCtr = LBound(myWkBks) To UBound(myWkBks)
> 'do more stuff
> Next iCtr
>
> End Sub
>
>
>
> Barb Reinhardt wrote:
> >
> > This question really relates to something I want to do in PowerPoint, but I'm
> > guessing that the same type of thing would apply in Excel as well.
> >
> > I'd like to identify four workbooks to open programmatically. I'd like to
> > assign them to oWB1, oWB2, oWB3 and oWB4 as they are opened. Can I refer to
> > them like this
> >
> > for i = 1 to 4
> > 'do things with workbook owb & i
> > next i
> >
> > Really what I'm doing is opening presentations and identifying them in a
> > similar way. The presentations are physically located on a server that takes
> > a while to access, so I don't want to open and close them repeatedly.
> >
> > Thanks

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      13th Nov 2006
dim myWBNames() as string
dim wCtr as long

wctr = 0
if something is true then
wctr = wctr +1
redim preserve mywbnames(1 to wctr)
mywbnames(wctr) = "c:\whateveryouneedhere"
end if

if wctr = 0 then
'no filenames found
else
'do the work
end if



Barb Reinhardt wrote:
>
> Dave,
>
> I won't know the workbook names until I do some other things. How do I
> dynamically create an array?
>
> Thanks,
> Barb
>
> "Dave Peterson" wrote:
>
> > I think I'd use an actual array:
> >
> > Option Explicit
> > Sub testme01()
> >
> > Dim myWBNames As Variant
> > Dim myWkBks() As Workbook
> > Dim iCtr As Long
> >
> > myWBNames = Array("C:\a.xls", "c:\b.xls", "C:\c.xls")
> >
> > ReDim myWkBks(LBound(myWBNames) To UBound(myWBNames))
> >
> > For iCtr = LBound(myWkBks) To UBound(myWkBks)
> > Set myWkBks(iCtr) = Workbooks.Open(Filename:=myWBNames(iCtr))
> > 'do more stuff right after opening
> > Next iCtr
> >
> > 'or wait until they're all open and do stuff then
> > For iCtr = LBound(myWkBks) To UBound(myWkBks)
> > 'do more stuff
> > Next iCtr
> >
> > End Sub
> >
> >
> >
> > Barb Reinhardt wrote:
> > >
> > > This question really relates to something I want to do in PowerPoint, but I'm
> > > guessing that the same type of thing would apply in Excel as well.
> > >
> > > I'd like to identify four workbooks to open programmatically. I'd like to
> > > assign them to oWB1, oWB2, oWB3 and oWB4 as they are opened. Can I refer to
> > > them like this
> > >
> > > for i = 1 to 4
> > > 'do things with workbook owb & i
> > > next i
> > >
> > > Really what I'm doing is opening presentations and identifying them in a
> > > similar way. The presentations are physically located on a server that takes
> > > a while to access, so I don't want to open and close them repeatedly.
> > >
> > > Thanks

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to store the reference of Workbooks in an array. Store a reference of a excel workbook Microsoft Excel Programming 4 6th Mar 2009 12:04 AM
Workbooks Array Otto Moehrbach Microsoft Excel Programming 7 11th Jan 2008 11:42 PM
Create an array of all open workbooks =?Utf-8?B?Sk5X?= Microsoft Excel Programming 3 24th Aug 2006 04:29 PM
Opening Workbooks / Filling Array =?Utf-8?B?QmlsbA==?= Microsoft Excel Programming 1 19th Jan 2006 08:19 PM
Use Array to activate workbooks Stuart Microsoft Excel Programming 3 2nd Dec 2003 08:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 PM.