Consulting

Results 1 to 5 of 5

Thread: Arrays and Accessing Elements by Index

  1. #1

    Question Arrays and Accessing Elements by Index

    I assume that I have 1 contiguous range starting in A1 that looks like the attached jpeg

    and use

    [VBA]Range(Range("A1"), ActiveCell.SpecialCells(xlLastCell)).Select
    [/VBA]

    to select the range. Once the range is selected I would like to create a 3 by 3 array with the correct dimensions based on the selected range

    Then I would like to be able to preform calculations on certain elements of the array. For example add items (1,1)+(2,2)+(3,3). The answer should be 8.6+77.7+0.9=87.1.

    I have very limited experience with arrays in vba and do not really know how to access elementes of >1D arrays. Thanks!

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The code you posted selects all of the .UsedRange and is equivilant to:
    ActiveSheet.UsedRange.Select
    If you want to create an array that has the values in the first three columns of the sheet,
    Dim myRange As Range
    Dim myArray As Variant
    With ActiveSheet
        Set myRange = Range(.Range("a1"), .Range("c65536").End(xlUp))
    End With
    myArray = myRange.Value
    will do the job. (If you only want the data without the headers use "a2" rather than "a1".)
    myArray will be a dimensioned (1 to n, 1 to 3) where n is the number of rows.
    I hope this helps.

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    A minor point, the line:
    [vba]Set myRange = Range(.Range("a1"), .Range("c65536").End(xlUp))[/vba] could, in this case, be reliably replaced with:
    [vba] Set myRange = Range(.Range("a2"), .Range("C1").End(xlDown))[/vba] since you've been explicit about it being a contiguous range. This way, you can afford to have something in column C below the table without upsetting the range.

    To do your calculation, this, after mikerickson's code:
    [vba]myresult = myArray(1, 1) + myArray(2, 2) + myArray(3, 3)
    MsgBox myresult[/vba]which rather than 87.1, gives 87.2 - naturally .
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you set a Range variable to contain your data, you can simply use a Range qualified cell reference to return the values. This example assumes a block of data, top left cell of which is F9

    [VBA]Sub Test()
    Dim MyRng As Range, Result As Single
    Set MyRng = Range("F9").CurrentRegion
    With MyRng
    Result = .Cells(2, 1) * .Cells(3, 2) * .Cells(4, 3)
    End With
    Range("A1") = Result
    End Sub

    [/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'

  5. #5
    thank you for all your responses they solved my problem

Posting Permissions

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