Hi,
1. I need to Open Excel file
2. Read all sheets into array, read sheet names into combo box
3. When user clicks on a sheet name in the combo box - show that sheet

This is how I'm doing it right now:

VB Code:
  1. Dim garrSheet1 As Variant        ' arrays will hold workbook's sheets
  2. Dim garrSheet2 As Variant
  3. Dim garrSheet3 As Variant
  4. Dim garrSheet4 As Variant
  5. Dim garrSheet5 As Variant
  6.  
  7. Private Sub ReadFile()
  8.   Me.MousePointer = vbHourglass
  9.  
  10.   On Error GoTo Leave
  11.  
  12.   Dim xlApp As Excel.Application
  13.   Dim wb As Workbook
  14.   Dim ws As Worksheet
  15.  
  16.   Dim iSheets As Integer, ctr As Integer
  17.  
  18.   Set xlApp = New Excel.Application
  19.   Set wb = xlApp.Workbooks.Open("c:\myFile.xls", 0)
  20.  
  21.   ' Get number of sheets in a workbook
  22.   iSheets = wb.Worksheets.Count
  23.  
  24.   ' Clear combo box before populating with actual sheets
  25.   cboSheet.Clear
  26.  
  27.   ' Go through the sheets, add their names to combo box and store them in arrays
  28.   For ctr = 1 To iSheets
  29.  
  30.     Set ws = wb.Worksheets(ctr)
  31.    
  32.     ' Get sheet name & add to combobox
  33.     cboSheet.AddItem ws.Name
  34.    
  35. [B]    ' Store each sheet in an array
  36.     Select Case ctr
  37.       Case 1:     garrSheet1 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20))
  38.       Case 2:     garrSheet2 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20))
  39.       Case 3:     garrSheet3 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20))
  40.       Case 4:     garrSheet4 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20))
  41.       Case 5:     garrSheet5 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20))
  42.     End Select  [/B]
  43.  
  44.   Next ctr
  45.  
  46.   ' Show default sheet
  47.   cboSheet.ListIndex = DEF_SHEET
  48.  
  49. Leave:
  50.   xlApp.Application.DisplayAlerts = False
  51.   wb.Close
  52.   xlApp.Quit
  53.    
  54.   Set ws = Nothing
  55.   Set wb = Nothing
  56.   Set xlApp = Nothing
  57.  
  58.   Me.MousePointer = vbNormal
  59. End Sub


This works but the number of sheet arrays must be hardcoded in the program and they must match number of sheets in the Excel. If I add another sheet to the Excel file I'll have to change the program to add another array. So, I'd like to make it more dynamic as I don't know how many sheets there are in the file.

I would really like to replace the bolded Select Case above with something like the code below which will hold all my sheets in one place regardless of how many sheets there are in the file, but I can't get it to work.
VB Code:
  1. [b]arrMySheets(ctr) = ws.Range(ws.Cells(1, 1), ws.Cells(50,20))[/b]

Any ideas greatly appreciated.