Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
JNW JNW is offline
external usenet poster
 
Posts: 480
Default Create an array of all open workbooks

I've never created an array, and after some research have decided I need help.

I need to make a list of all open workbooks (with the exception of
personal.xls) and populate a listbox (on a userform) with this list of open
workbooks.

Then I need to be able to select multiple workbooks from the listbox and
capture the name of each of the select workbooks as a different variable for
later use.

Thanks for the help!


--
JNW
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Create an array of all open workbooks

I put this in a General Module:

Option Explicit
Public WkbkNames() As String
Public SomeWkbkWasSelected As Boolean

I created a userform with 2 buttons (ok/cancel) and a listbox.

This was the code behind the userform:

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
Dim iCtr As Long
Dim wkbkCtr As Long
SomeWkbkWasSelected = False
wkbkCtr = -1
With Me.ListBox1
ReDim WkbkNames(0 To .ListCount - 1)
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) = True Then
SomeWkbkWasSelected = True
wkbkCtr = wkbkCtr + 1
WkbkNames(wkbkCtr) = .List(iCtr)
End If
Next iCtr
End With
ReDim Preserve WkbkNames(0 To wkbkCtr)

Unload Me

End Sub
Private Sub UserForm_Initialize()
Dim wkbk As Workbook
Dim myWin As Window

Me.ListBox1.MultiSelect = fmMultiSelectMulti
For Each wkbk In Application.Workbooks
For Each myWin In wkbk.Windows
If myWin.Visible = True Then
Me.ListBox1.AddItem wkbk.FullName
Exit For
End If
Next myWin
Next wkbk
End Sub

=======
I could use this in any subsequent routine:

Option Explicit
Sub testme()

Dim iCtr As Long

If SomeWkbkWasSelected = True Then
For iCtr = LBound(WkbkNames) To UBound(WkbkNames)
MsgBox WkbkNames(iCtr)
Next iCtr
End If

End Sub

I'm not sure if you wanted to hold those values or just use them in the ok
button's click event.

JNW wrote:

I've never created an array, and after some research have decided I need help.

I need to make a list of all open workbooks (with the exception of
personal.xls) and populate a listbox (on a userform) with this list of open
workbooks.

Then I need to be able to select multiple workbooks from the listbox and
capture the name of each of the select workbooks as a different variable for
later use.

Thanks for the help!

--
JNW


--

Dave Peterson
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
When I open Excel, workbooks open automatically. How can I stop t Rhealbird Excel Discussion (Misc queries) 2 February 23rd 06 10:08 AM
workbooks.open function fails to open an existing excel file when used in ASP, but works in VB. san Excel Programming 1 January 3rd 06 03:22 AM
Excel 2003 Workbooks.Open with CorruptLoad=xlRepairFile fails on Excel 5.0/95 file due to Chart, with Error 1004 Method 'Open' of object 'Workbooks' failed Frank Jones Excel Programming 2 June 15th 04 03:21 AM
Workbooks.Open closes other workbooks S. Daum Excel Programming 1 August 21st 03 07:47 PM
Workbooks.Open / .Open Text - How do you stop the .xls addition? Dave[_20_] Excel Programming 2 July 31st 03 04:03 AM


All times are GMT +1. The time now is 09:06 PM.

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

About Us

"It's about Microsoft Excel"