Sub AssignToVariables()
Dim Sary As Variant, Dary As Variant
Dim i As Long
Sary = Split(Range("A1").Value, ";")
'*****************
ReDim Dary(0 To UBound(Sary)) As Double
For i = 0 To UBound(Sary)
Dary(i) = Sary(i)
Next i
End Sub
Please post the code that you are referring to and explain what you are trying to do with that code. Also, make it clear which line of the code is giving that error.Please help to resolve this.
Dim m As Variant, n As Variant, p As Variant, q As Variant[/FONT]
[FONT=arial]Dim ary As String[/FONT]
[FONT=arial]Sheets("Sheet2").Select[/FONT]
[FONT=arial]a = 2[/FONT]
[FONT=arial]b = 3[/FONT]
[FONT=arial]For a = 2 To 10[/FONT]
[FONT=arial]ary = Split([/FONT][FONT=arial]ActiveSheet.Cells(a, b - 1).Value[/FONT][FONT=arial], ";")[/FONT]
[FONT=arial] m = ary(0)[/FONT]
[FONT=arial] n = ary(1)[/FONT]
[FONT=arial] p = ary(2)[/FONT]
[FONT=arial]'....
[/FONT]'...
'... does few calculations
'...
'...
[FONT=arial]Sheet2.Cells(a, b).Value = q[/FONT]
[FONT=arial]Next a
[/FONT]
By my reckoning that code should not even work for one iteration. If using ary as a variable to receive the result of a split function, ary should be declared as Variant, not String. As it is written it will not run at all for me "Compile error: expected array" with m=ary(0) highlighted.This works for the first iteration.
That would indicate that cell B3 on the active sheet is empty.For the second it throws "Subscript out of range" error pointing to m = ary(0)
It is working for me. Let's simplify to start with & try this.No luck
I changed the data type to Variant. When I give individual cell references, say B3 (3,2), it works perfectly well. When I run it in for loop, it does the calculation for the B2 and then for B3 it repeats the error "Subscription out of range" pointing to m = ary(0).
Excel Workbook | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | |||||||
2 | 1;3;2.3 | ||||||
3 | 5;8;9 | ||||||
4 | 6;4;8 | ||||||
5 | |||||||
Sheet2 |
Sub AssignToVariables()
Dim m As Variant, n As Variant, p As Variant, ary As Variant
Dim a As Long, b As Long
Sheets("Sheet2").Select
b = 3
For a = 2 To 4
ary = Split(Cells(a, b - 1).Value, ";")
m = ary(0)
n = ary(1)
p = ary(2)
Cells(a, b).Resize(, 3).Value = Array(m, n, p)
Next a
End Sub
Excel Workbook | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | |||||||
2 | 1;3;2.3 | 1 | 3 | 2.3 | |||
3 | 5;8;9 | 5 | 8 | 9 | |||
4 | 6;4;8 | 6 | 4 | 8 | |||
5 | |||||||
Sheet2 |