PDA

View Full Version : Solved: assigning values to variable using loops



chungtinhlak
01-19-2009, 05:08 PM
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.


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


Thanks

Bob Phillips
01-19-2009, 06:10 PM
No, use an array



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

m(i) = Range("y" & i).Value
Next i
End Sub

Ischyros
01-19-2009, 06:15 PM
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".


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


Let me know if this is what you wanted.

chungtinhlak
01-20-2009, 09:35 AM
thank you