Results 1 to 3 of 3

Thread: transfer an Excel-Cell-Range into a 2D-Variant-array and perform sorting

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question transfer an Excel-Cell-Range into a 2D-Variant-array and perform sorting

    Hi, i have been working on best seller report . problem is that user is getting
    each week a Trading Excel sheet with more that 50,000 lines and 67 cols .
    i have written code to write data from the excel it takes such a long time . approx 2 hour to read only 10 cols data with 50,000 lines from the excel sheet and store in the collection .because after filling into collection.i would like to sort the week sold value in ascending order.keep top 10 value.
    and get it at other excel sheet .but it is time consuming process .as schiff suggest me .
    excel support direct assignment from excel worksheet to 2d array .and i don't want to touch Excel remote com till it finish writing all the lines in excel sheet .and it seems logical . keeping in 2d variant array so i would like to ask from shiff ."How to transfer an Excel-Cell-Range into a 2D-Variant-array
    and perform sorting .because i have been transfer from Excel-cell-range to collection . filling into the
    collection is really time consuming .so i would like to fill in the 2d variant array.
    and then sort data on the basis of week sold value to get best seller report .
    Code:
    Private Sub BtCreate_Click()
    Dim txt As String
    Dim RowCount As Long
    Dim colCount As Long
    Dim oTradingClass As Trading
    Dim x As Long
    Dim y As Long
    If Not Checkinput Then Exit Sub
       Set wb = excl.Workbooks.Open(txtSourceFile.Text)  ' here we test for the gotten adress
       If Not wb Is Nothing Then
         excel.Application.Visible = False
         Set ws = wb.Worksheets(1)
         RowCount = ws.UsedRange.Rows.Count
    '     colCount = ws.UsedRange.Columns.Count
        x = 1
        Do
           txt = ws.Columns.Cells(x, 1)
           If txt = "Barcode" Or txt = "BARCODE" Then Exit Do
          
          
          x = x + 1
         Loop
         'next line
         x = x + 1
         Do
            'One or more empty Rows
            txt = ws.Columns.Cells(x, 1)
             If txt = "" Then
             x = x + 1
            Else
              Exit Do
            End If
         Loop
         y = 1
         Do
         
           Set oTradingClass = New Trading
           oTradingClass.Key = "K" + CStr(y)
           oTradingClass.ItemDesc = ws.Cells(x, 5).Value
           oTradingClass.ColorDesc = ws.Cells(x, 7).Value
           oTradingClass.DeptName = ws.Cells(x, 11).Value
           oTradingClass.WeekSoldQty = ws.Cells(x, 20).Value
           oTradingClass.WeekSoldCost = ws.Cells(x, 21).Value
           oTradingClass.WeekSoldValue = ws.Cells(x, 22).Value
           oTradingClass.PeriodRetail = ws.Cells(x, 37).Value
           oTradingClass.WeekCover = ws.Cells(x, 45).Value
           oTradingClass.DcStock = ws.Cells(x, 52).Value
           oTradingClass.ClosingQty = ws.Cells(x, 54).Value
         Call TradingSummaryCol.Add(oTradingClass)
           ' now next line
            x = x + 1
            y = y + 1
           Loop
           Debug.Print TradingSummaryCol.Count
           excel.Application.Visible = True
       Else
         Call MsgBox("Excel is not open", vbInformation, "Test")
       End If
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: transfer an Excel-Cell-Range into a 2D-Variant-array and perform sorting

    how about using an sql query? can filter and sort into a recordset
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: transfer an Excel-Cell-Range into a 2D-Variant-array and perform sorting

    I don't see any Exit-Condition in the above loop, which fills your oTrading-objects.

    aside from that, you can avoid referencing the single cells over each and every Row and Column,
    when you gather the Column-Data for each of your (index-wise) "scattered" Cols
    in one Call into a properly named VB-Variant-array:

    E.g. you can "gobble up" your Column-Content which contains 'ColorDesc'
    (e.g. for Row-Indexes 1 to 65536) with the single Call:

    Dim ArrColorDesc() As Variant
    ArrColorDesc = ws.Range("K1:K65536")

    So the above beams the Cell-Contents into the Array in one go -
    and the Array will have the Bounds: (1 to 65536, 1 to 1) then.

    Just do that for all your other 9 "named columns" as well - in dedicated
    Arrays, which have "speaking names" (ArrItemDesc(), ArrDeptName(), etc.)

    After you copied this "single Column-Content" over into your 9 Arrays,
    you can then just loop over the Row-Indexes of the Arrays instead (avoiding
    any further single XL-Cell-Addressing over Remote-COM).

    HTH

    Olaf

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