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.

    [vba]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[/vba]

    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 differnt type of data and this is a simple syntax issue. Any help appreciated.

    Cheers!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    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,489
    Location
    You can use Match instead of checking each value
    [VBA]
    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
    [/VBA]
    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,722
    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,489
    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
  •