1

I'm new to VBA in excel, I write code for copying cells from sheet to an array. When I run I got run time error. I don't know whats wrong.

Sub DistSystem()

Dim count As Integer    
Dim i As Integer    
Dim array_rank() As Variant    
Dim array_city() As Variant    
Dim array_assign() As Variant

    count = Sheets("111").Range("Y2").Value

    For i = 0 To count
        array_city(i) = Range("A" & i).Value    
        array_rank(i) = Range("E" & i).Value
        array_assign(i) = Range("F" & i).Value
    Next

    For i = 1 To 10
        MsgBox array_rank(i, 1) 
    Next

End Sub
1
  • 1
    What runtime error do you get, and which line. These pieces of information a vital to anyone trying to solve you problem, so always should be included. Commented Jun 4, 2014 at 8:33

1 Answer 1

3

I suspect you are going to be battle errors in multiple places.

This section of code has 2 significant problems

array_city(i) = Range("A" & i).Value    
array_rank(i) = Range("E" & i).Value
array_assign(i) = Range("F" & i).Value

First you are trying to assign values to array that do not have any dimensions. You decleared the arrays but you left them dimensionless. You need to define the dimensions before you try to assign values to the array

Something like

Redim array_city(1 to count)

Next you are trying to get a value from Range("A" & i) when the value of i is zero. Cell "A0" does not exists and will also throw an error.

So to rewrite your code as written, you would need to make a few changes:

Sub DistSystem()

Dim count As Integer    
Dim i As Integer    
Dim array_rank() As Variant    
Dim array_city() As Variant    
Dim array_assign() As Variant

    count = Sheets("111").Range("Y2").Value

    Redim array_rank(1 to count)
    Redim array_city(1 to count)    
    Redim array_assign(1 to count)

    For i = LBound(array_rank) To UBound(array_rank)
        array_city(i) = Range("A" & i).Value    
        array_rank(i) = Range("E" & i).Value
        array_assign(i) = Range("F" & i).Value
    Next

    For i = 1 To 10
        MsgBox array_rank(i) 
    Next

End Sub

However, you are over complicating how you are reading the values into the array. You can simply read the entire range directly into the array

Sub DistSystem()

Dim count As Integer    
Dim i As Integer    
Dim array_rank As Variant    'Notice the arrays are not longer declared with () 
Dim array_city As Variant    '  -> this is necessary
Dim array_assign As Variant

    count = Sheets("111").Range("Y2").Value

    array_city = Range("A1:A" & count).Value    
    array_rank = Range("E1:E" & count).Value
    array_assign = Range("F1:F" & count).Value

    For i = 1 To 10
        MsgBox array_rank(i, 1) 
    Next

End Sub

The resulting array with be 2 dimensions, with the Row value as the first dimension and the column as the 2nd dimension. since all of the ranges are a single column, you would access any value by calling array_rank(Row,1) or array_city(Row,1) or array_assign(Row,1).

Sign up to request clarification or add additional context in comments.

1 Comment

psubsee hope you can help me with my issue I just asked before hour ago stackoverflow.com/questions/24035395/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.