1

I am trying to store the values inside an array. I am facing a problem it says subscript out of range.

This is the code,

Sub Trial()

Dim HeaderArray() As Variant

Dim HeaderValue As String

Dim j As Long

Dim i as Long

set wk = Activeworkbook

lastrow_header_Config = Wk.Sheets("Config").Cells(Rows.Count, "W").End(xlUp).Row

j = 1

      For i = 2 To lastrow_header_Config

         HeaderValue = Wk.Sheets("Config").Range("W" & i).Value

             If HeaderValue <> "" Then

              HeaderArray(j - 1) = HeaderValue // Subscript out of range error

             j = j + 1

             End If

      Next

End Sub

What is the mistake I am making. Kindly advise.

6
  • 3
    Ouch, please read up on indenting your code. Which line errors? Commented Sep 19, 2017 at 13:56
  • 4
    You do not set the size of the array. You need to Redim HeaderArray(the size you want) Commented Sep 19, 2017 at 13:56
  • 7
    use option Explicit! Commented Sep 19, 2017 at 13:58
  • @Scott Craner, Ya that helped. But its going to be dynamic. So, how do we make it as dynamic. Commented Sep 19, 2017 at 13:59
  • Make it dynamic with ReDim Preserve. Or set size with variable before loop. Commented Sep 19, 2017 at 14:01

3 Answers 3

4

You need to declare the size of the array before trying to put data in it. Use COUNTA to find the number of cells with data in your range:

Sub Trial()

Dim HeaderArray() As Variant
Dim HeaderValue As String
Dim lastrow_Header_Config As Long
Dim j As Long
Dim i As Long

Set Wk = ActiveWorkbook

lastrow_Header_Config = Wk.Sheets("Config").Cells(Rows.Count, "W").End(xlUp).Row
ReDim HeaderArray(Application.WorksheetFunction.CountA(Wk.Sheets("Config").Range("W2:W" & lastrow_Header_Config))-1) As Variant
j = 0

For i = 2 To lastrow_Header_Config
    HeaderValue = Wk.Sheets("Config").Range("W" & i).Value
    If HeaderValue <> "" Then
        HeaderArray(j) = HeaderValue
        j = j + 1
    End If
Next

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

Comments

3

try this and see how it works for you

pay close attention to the ReDim HeaderArray(j) line and the ReDim Preserve HeaderArray(j) lines

Sub Trial()
    Dim HeaderArray() As Variant
    Dim HeaderValue As String
    Dim j As Long
    Dim i As Long
        Set Wk = ActiveWorkbook
        lastrow_header_Config = Wk.Sheets("Config").Cells(Rows.Count, "W").End(xlUp).Row
        j = 1
        ReDim HeaderArray(j)   '<============= initialize your array length
        For i = 2 To lastrow_header_Config
            HeaderValue = Wk.Sheets("Config").Range("W" & i).Value
            If HeaderValue <> "" Then
                ReDim Preserve HeaderArray(j) '<================= adjust your array length to accomodate the additional info
                HeaderArray(j - 1) = HeaderValue '// Subscript out of range error
                j = j + 1
            End If
        Next
End Sub

Also you might want to read up on using the option keyword. Arrays by default have the first data point at index 0 so for example array(1) creates an array that has 1 data point, however to reference that data point you would use array(0). if you wanted the first data point in the array to be referenced using array(1), then you would use the Option Base 1 keyword at the very top of your module.

1 Comment

Nice Option. FWIW; Redim Preserve is slower than setting the size before hand. But in many instances it is the only option. Another note about it is that in multi dimensional arrays you can only change the last dimensions. The others are set when initialized.
1

On the first pass, j = 1. Therefore you try to set HeaderArray(0) a value, while HeaderArray is probably 1 based.
You can eventually use Option Base 0, or explicitely Redim HeaderArray(0 to 10) (or whatever value you need)

5 Comments

But array starts from '0' ? isn't it ?
No they don't. It depends on your option base. Except for Split() which always returns a 0 based array, if I remember well.
And when loading an array directly from a range it starts at 1: SomeArray = Range("A1:A10").Value will start at 1. @Sid29
Thanks guys :) learned more than what I wanted to, really nice of you :)
Implicitly-sized arrays are 0-based unless specified otherwise (and yes, except if it's initialized from a Range). Option Base 0 is redundant, it's the default.

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.