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:
Dim garrSheet1 As Variant ' arrays will hold workbook's sheets Dim garrSheet2 As Variant Dim garrSheet3 As Variant Dim garrSheet4 As Variant Dim garrSheet5 As Variant Private Sub ReadFile() Me.MousePointer = vbHourglass On Error GoTo Leave Dim xlApp As Excel.Application Dim wb As Workbook Dim ws As Worksheet Dim iSheets As Integer, ctr As Integer Set xlApp = New Excel.Application Set wb = xlApp.Workbooks.Open("c:\myFile.xls", 0) ' Get number of sheets in a workbook iSheets = wb.Worksheets.Count ' Clear combo box before populating with actual sheets cboSheet.Clear ' Go through the sheets, add their names to combo box and store them in arrays For ctr = 1 To iSheets Set ws = wb.Worksheets(ctr) ' Get sheet name & add to combobox cboSheet.AddItem ws.Name [B] ' Store each sheet in an array Select Case ctr Case 1: garrSheet1 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20)) Case 2: garrSheet2 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20)) Case 3: garrSheet3 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20)) Case 4: garrSheet4 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20)) Case 5: garrSheet5 = ws.Range(ws.Cells(1, 1), ws.Cells(50,20)) End Select [/B] Next ctr ' Show default sheet cboSheet.ListIndex = DEF_SHEET Leave: xlApp.Application.DisplayAlerts = False wb.Close xlApp.Quit Set ws = Nothing Set wb = Nothing Set xlApp = Nothing Me.MousePointer = vbNormal 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:
[b]arrMySheets(ctr) = ws.Range(ws.Cells(1, 1), ws.Cells(50,20))[/b]
Any ideas greatly appreciated.




Reply With Quote