variant in vba


I am fetching the last row and storing that in an integer. For example, if my last row is 10 I want to create a two dimensional matrix with  values in the range  A1:D(lastRow) . The values in the range will be stored and then I will loop through each row and apply a function to it. How do I code for this?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Just do

Dim DataRange As Variant

And you can put anything you like in it, no matter the source dimensions.
Martin LissOlder than dirtCommented:

DataRange = Range("A1:D" & Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row)
Saurabh Singh TeotiaCommented:
I'm assuming this is what you are looking for..

Sub checkrange()
    Dim cell As Range, rng As Range
    Dim lrow As Long

    lrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row

    Set rng = Range("A1:A" & lrow)

    For Each cell In rng

        ' Your code here now since you can read each value by reading cell.value
        'to read column B value it will be cell.offset(0,1).value and for c replce 1 by 2 and so on

    Next cell
End Sub

Open in new window

Kanti PrasadCommented:

This will get the last row and create a two-dimensional matrix with  values of that range and you can  loop through each row and change it. Here I am appending 99

Sub StoreMatrix()
    Dim MyArray, aSheet As Worksheet, rCnt As Long, cCnt As Integer
    Dim ValueChangedArray, ArrayValue As Variant, ChangedValue As Variant
    Set aSheet = ActiveSheet
    Set Rng = sh.Range(sh.Range("A1"), sh.Range("D" & Cells.Rows.Count).End(xlUp))
    With ActiveSheet.Range(Rng)
    ReDim MyArray(1 To .Rows.Count, 1 To .Columns.Count)
    ReDim ValueChangedArray(1 To .Rows.Count, 1 To .Columns.Count)
    MyArray = Sheet1.Range(Rng).Value
    For rCnt = 1 To .Rows.Count

        For cCnt = 1 To .Columns.Count
            Debug.Print MyArray(rCnt, cCnt)
           ArrayValue = MyArray(rCnt, cCnt)
           ChangedValue = ArrayValue & 99
            ValueChangedArray(rCnt, cCnt) = ChangedValue
            Debug.Print ValueChangedArray(rCnt, cCnt)
    Next: Next
        End With

End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.