Consulting

Results 1 to 4 of 4

Thread: Solved: assigning values to variable using loops

  1. #1
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    244
    Location

    Solved: assigning values to variable using loops

    Can i do this? it gives me an error at "M" & i. I tried it without " ", and () and everything, just can't figure it out.

    [VBA]
    Sub calcution()
    Dim m1 As Long, m2 As Long, m3 As Long, m4 As Long, m5 As Long, _
    m6 As Long, m7 As Long, m8 As Long, m9 As Long, m10 As Long, m11 As Long, m12 As Long, m13 As Long, m14 As Long
    For i = 1 To 14
    "M" & i = range("y" & i).Value

    End Sub
    [/VBA]

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, use an array

    [vba]

    Dim m(1 To 14) As Long
    For i = 1 To 14

    m(i) = Range("y" & i).Value
    Next i
    End Sub[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Nov 2008
    Posts
    34
    Location
    This code below assigns values to the array m. It moves down row two column by column assigning a value to each array element. The Base 1 option just sets the first array element at "1" instead of "0".

    [VBA]
    Option Base 1
    Sub calculation()
    Dim i As Integer, j As Integer
    Dim m(1 To 14) As Double
    i = 1
    j = 1
    For i = 1 To 14
    m(i) = Worksheets(1).Cells(2, j).Value
    MsgBox (m(i))
    j = j + 1
    Next i
    End Sub
    [/VBA]

    Let me know if this is what you wanted.

  4. #4
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    244
    Location
    thank you

Posting Permissions

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