|
-
Apr 20th, 2005, 01:11 PM
#1
Thread Starter
Hyperactive Member
Storing Excel sheets in array
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.
-
Apr 21st, 2005, 07:00 AM
#2
Thread Starter
Hyperactive Member
Re: Storing all Excel sheets in one array
-
Apr 21st, 2005, 12:26 PM
#3
Thread Starter
Hyperactive Member
Re: Storing Excel sheets in array
One more time, then I'm looking for a bridge...
-
Apr 21st, 2005, 01:03 PM
#4
Re: Storing Excel sheets in array
Why are you loading all the sheet data into an array if the sheet is going to be displayed anyways?
For the hardcoding of the array you can use ReDim Preserve arrMySheets(SomeVariableCountValue +1) As Variant
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 21st, 2005, 01:50 PM
#5
Thread Starter
Hyperactive Member
Re: Storing Excel sheets in array
Well, I want to store the sheets in an array because they'll be displayed based on selection in a combo box. This way the population of the grid is fast and user doesn't have to wait. Plus I want to do some formating before showing in the grid.
Also, I'm getting "Type mismmatch" error on this line:
VB Code:
ReDim Preserve arrMySheets(SomeVariableCountValue +1) As Variant
-
Apr 21st, 2005, 02:02 PM
#6
Re: Storing Excel sheets in array
Oh, ok. The error is because I just used a bogus variable for an example. You need a var with a value in there.
Something like sheet count, but if your adding sheets then you need to redim it with the + 1. If you need to redim your
sheet array size then that is a two dimentional array. You will need to get the max rows and max columns for the
range and redim on that, I think.
But since your already opening the excel workbook, why not just leave it open but hidden. Then when the user
clicks on the combo youjust have to unhide it. Allot faster.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 21st, 2005, 02:18 PM
#7
Thread Starter
Hyperactive Member
Re: Storing Excel sheets in array
I can't leave it open because my program checks for newer xl file once per hour and if there, loads it into arrays to be displayed as needed.
I did change the redim to have a meaningfull variable but doesn't like it.
I'm not sure if you can redim an array that has a range in it like so:
VB Code:
For ctr = 1 To iSheets
Set ws = wb.Worksheets(ctr)
ReDim Preserve arrMySheets(ctr, MAX_ROWS, MAX_COLS)
arrMySheets(ctr) = ws.Range(ws.Cells(1, 1), ws.Cells(MAX_ROWS, MAX_COLS))
Next ctr
-
Apr 21st, 2005, 02:27 PM
#8
Re: Storing Excel sheets in array
I double checked and you can only redim on the last element of the array.
So if you had an array ar(10, 10, 10) and you redimmed preserved it - Redim ar(10, 10 , 15) would only be valid.
You basically need to Erase the array variable and then redim it to the larger size but all data will need to be reloaded
into the array.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|