Results 1 to 8 of 8

Thread: Storing Excel sheets in array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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:
    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.
    Thanks

    Tomexx.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Storing all Excel sheets in one array

    Anybody...?
    Thanks

    Tomexx.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Storing Excel sheets in array

    One more time, then I'm looking for a bridge...
    Thanks

    Tomexx.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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:
    1. ReDim Preserve arrMySheets(SomeVariableCountValue +1) As Variant
    Thanks

    Tomexx.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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:
    1. For ctr = 1 To iSheets
    2.  
    3.     Set ws = wb.Worksheets(ctr)
    4.  
    5.       ReDim Preserve arrMySheets(ctr, MAX_ROWS, MAX_COLS)
    6.       arrMySheets(ctr) = ws.Range(ws.Cells(1, 1), ws.Cells(MAX_ROWS, MAX_COLS))
    7.  
    8.   Next ctr
    Thanks

    Tomexx.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width