Results 1 to 14 of 14

Thread: How Do I: Use a Spreadsheet as an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    9

    How Do I: Use a Spreadsheet as an array

    I Have A Bunch of data on a spreadsheet that i would like to use to fill in values for checkboxes, comboboxes, listboxes, sliders, and textboxes. each item starts anew at A and ends at AN and there are 88 items as of now. I will be assigning variables from each field A-AN depending on which item is selected. Can anyone get me started.

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Yoy should start by doing a forum (or web) search. At any rate, here's a post about interfacing with Excel.
    http://www.vbforums.com/showthread.p...gramming+excel
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    9
    Thanks I'll give it a shot

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    9
    Yeah.... No help.
    i need a link to a snipet for taking values FROM Excel Spreadsheet A1 thru A88 and assigning them to VB.
    Here's the error i get

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    9
    sorry for double posting
    here's a screenshot
    Attached Images Attached Images  

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Here is a place to start, if this is on track, I'll add the Array loading (on is already Dimmed).


    Add the 'name' of your File to the Open statement too

    Don't close of the instance of Excel, just Minimise it (I'll do that latter)
    VB Code:
    1. Option Explicit
    2.  
    3. 'Note: NO reference to MS Excel Object Library x.x is required in this example
    4.  
    5. Private Sub Form_Load()
    6. Dim objExcel As Object
    7. Dim objExcelWS As Object
    8. Dim intRowCount As Integer, intColCount As Integer
    9. Dim intRowIdx As Integer, intColIdx As Integer
    10. Dim strArr() As String
    11.  
    12. On Error GoTo Err_Handler
    13.  
    14.     'Use the current (Opened) existance of Excel << Alternate method
    15.     'Set objExcel = GetObject(, "Excel.Application")
    16.  
    17.     'Craete an Instance of Excel
    18.     Set objExcel = CreateObject("Excel.Application")
    19.     Set objExcelWS = objExcel.Workbooks.Open(App.Path & "\test.xls")
    20.  
    21.     objExcel.Visible = True
    22.  
    23.     intRowCount = objExcel.ActiveSheet.UsedRange.Rows.Count 'Capture the number of USED Rows
    24.     intColCount = objExcel.ActiveSheet.UsedRange.Columns.Count  'Capture the number of USED Columns
    25.  
    26.     For intRowIdx = 1 To intRowCount  'Iterate each Row
    27.         For intColIdx = 1 To intColCount  'Iterate each Column
    28.             MsgBox objExcel.ActiveSheet.Cells(intRowIdx, intColIdx).Value
    29.         Next
    30.     Next
    31.  
    32.     objExcel.Application.Quit
    33.     Set objExcel = Nothing
    34.     Set objExcelWS = Nothing
    35.  
    36. Exit Sub
    37.  
    38. Err_Handler:
    39.     If Not (objExcelWS Is Nothing) Then Set objExcelWS = Nothing
    40.     If Not (objExcel Is Nothing) Then
    41.         objExcel.Application.Quit
    42.         Set objExcel = Nothing
    43.     End If
    44.  
    45.     MsgBox "Number: " & Err.Number & vbCrLf & _
    46.     "Description: " & Err.Description, vbOKOnly + vbCritical, "Error!"
    47. End Sub




    Bruce.

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    The Excel Rows and Columns are 'dynamic'. You can add/delete as you need and the code will still work!




    Bruce.

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    The strArr is MultiDimensional. It now stores all the values of the WorkSheet

    Array(0,0) = A1
    Array(0,1) = B2
    .
    .
    Array(1,0) = A2
    etc.

    VB Code:
    1. Option Explicit
    2.  
    3. 'Note: NO reference to MS Excel Object Library x.x is required in this example
    4.  
    5. Private Sub Form_Load()
    6. Dim objExcel As Object
    7. Dim objExcelWS As Object
    8. Dim intRowCount As Integer, intColCount As Integer
    9. Dim intRowIdx As Integer, intColIdx As Integer
    10. Dim strArr() As String
    11.  
    12. On Error GoTo Err_Handler
    13.  
    14.     'Use the current (Opened) existance of Excel << Alternate method
    15.     'Set objExcel = GetObject(, "Excel.Application")
    16.  
    17.     'Craete an Instance of Excel
    18.     Set objExcel = CreateObject("Excel.Application")
    19.     Set objExcelWS = objExcel.Workbooks.Open(App.Path & "\test.xls")
    20.  
    21.     objExcel.Visible = True
    22.  
    23.     intRowCount = objExcel.ActiveSheet.UsedRange.Rows.Count 'Capture the number of USED Rows
    24.     intColCount = objExcel.ActiveSheet.UsedRange.Columns.Count  'Capture the number of USED Columns
    25.  
    26.     ReDim strArr(intRowCount - 1, intColCount - 1)
    27.  
    28.     For intRowIdx = 1 To intRowCount  'Iterate each Row
    29.         For intColIdx = 1 To intColCount  'Iterate each Column
    30.            strArr(intRowIdx - 1, intColIdx - 1) = objExcel.ActiveSheet.Cells(intRowIdx, intColIdx).Value
    31.         Next
    32.     Next
    33.  
    34.     objExcel.Application.Quit
    35.     Set objExcel = Nothing
    36.     Set objExcelWS = Nothing
    37.  
    38.     '**************************************
    39.     'Display the First value!
    40.     MsgBox strArr(0, 0)
    41.  
    42. Exit Sub
    43.  
    44. Err_Handler:
    45.     If Not (objExcelWS Is Nothing) Then Set objExcelWS = Nothing
    46.     If Not (objExcel Is Nothing) Then
    47.         objExcel.Application.Quit
    48.         Set objExcel = Nothing
    49.     End If
    50.  
    51.     MsgBox "Number: " & Err.Number & vbCrLf & _
    52.     "Description: " & Err.Description, vbOKOnly + vbCritical, "Error!"
    53. End Sub




    Bruce.
    Last edited by Bruce Fox; Sep 23rd, 2004 at 06:00 PM.

  9. #9
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Unless A1 and A88 are variables shouldn't the line (from Syizik's bitmap post)

    Set oRng1 = oWS.Range(A1, A88)

    be

    Set oRng1 = oWS.Range("A1", "A88")

    or the same syntax as oRng2

    Set oRng1 = oWS.Range("A1:A88")

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    I beleive you would be correct!

    In the example I posted, its is dynamic as far as number of rows/columns are concerned. It is help full to be ablr to return UsedRange - it is overlooked




    Bruce.

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    9
    It Worked!!!!!

    Tanks 4 ur support.

  12. #12
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    No problem


    Note: You can also use this (with a slight mod) to use the Excel Library Reference. In the example above, you don't need it - just have Excel of any version on the host PC.



    Bruce.

  13. #13

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    9
    How do i use this "UsedRange" Function you mentioned? Does it tell you how far it read the sheet? i.e.-max cells and rows

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by Syizik
    How do i use this "UsedRange" Function you mentioned? Does it tell you how far it read the sheet? i.e.-max cells and rows
    Ummm, Its in the code example I provided (I even commeneted those lines)

    UsedRage (column/row) will return the upper limit of the used cells. As previously mentioned it enables addition data/edits to the
    WorkSheet whithout any 'hardcoding' changes.




    Bruce.

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