Consulting

Results 1 to 5 of 5

Thread: Filling an array with a for..each construct

  1. #1

    Filling an array with a for..each construct

    Hi,

    I am trying to fill an array with a for..each cosntruct. I am just hoping this will speed up the process as a for..next combined with a paste in range is rather slow.

    here is the code I have.

    Sub test_sector_array()
        Dim totsect(1 To 24, 1 To 2, 1 To 50) As Variant
        Dim i, j As Integer
        Dim MyArr As Variant
        MyArr = Array("Automobiles & Components", "Banks", "Capital Goods", "Commercial & Professional Serv", "Consumer Durables & Apparel", _
        "Consumer Services", "Diversified Financials", "Energy", "Food & Staples Retailing", "Food Beverage & Tobacco", "Health Care Equipment & Servic", _
        "Household & Personal Products", "Insurance", "Materials", "Media", "Pharmaceuticals, Biotechnology", "Real Estate", "Retailing", _
        "Semiconductors & Semiconductor", "Software & Services", "Technology Hardware & Equipmen", "Telecommunication Services", "Transportation", "Utilities")
        For i = 1 To 24
            j = 1
            For Each cell In Range("E20", Range("E20").End(xlDown))
                If cell.Value = MyArr(i) Then
                    totsect(i, 1, j) = cell.Offset(0, -3)
                    totsect(i, 2, j) = cell.Offset(0, 4)
                    j = j + 1
                End If
            Next cell
        Next i
    End Sub
    I am getting the error at the red line. When I use the debug.print MyArr(i) I do not get any problem. I suppose the line tries to compare two different type of data and this is a simple syntax issue. Any help appreciated.

    Cheers!
    Last edited by Aussiebear; 11-21-2024 at 02:13 AM.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Can you post a sample workbook?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    You can use Match instead of checking each value
     
    For Each cell In Range("E20", Range("E20").End(xlDown))
        j = j + 1
        i = Application.Match(cell.Value, MyArr)
        totsect(i, 1, i) = cell.Offset(0, -3)
        totsect(i, 2, j) = cell.Offset(0, 4)
        Next cell
    Last edited by Aussiebear; 11-21-2024 at 02:13 AM.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,800
    Location
    What error are you actually getting? If it's a Type Mismatch, do you have any error values in those cells?

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Re the error, probably caused by MyArr being 0 to 23 (default), not 1 to 24, so no MyArr(24) exists.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •