1

I'm having a problem populating an array. For some reason all of the tutorials I can find would rather list the data & sizing explicitly in the code itself (which sort of defeats the purpose of using VBA to begin with for this task).

My question is about how to dynamically size an array, if it's even possible to do so in the manner that I'm trying to. The code itself is commented with the background and specific questions.

When I run the code I get an "Application-defined or object-defined error" (1004)

Also, using a for... next loop to populate the arrays may end up being computationally inefficient as the datasets typically average 23k rows

Code:

Sub DCList()
ReDim DCList(0, 0)

Dim cnum As Integer

' count the number of columns that have been used in the workbook
cnum = ActiveWorkbook.Sheets("data").UsedRange.Columns.Count

' set array dimensions to 1 row & Cnum columns
ReDim DCList(1, cnum)


' set array values to values found in the range (A1, cnum 1)
DCList = ActiveWorkbook.Sheets("data").Range(Cells(1, 1), Cells(1, cnum)).Value


'Other info:
' DCList is a global variable set as Variant type
' The overarching goal of this sub is purely to determine the column names of
' a dataset so that (in another sub) the user can select which column to lookup



End Sub
2
  • Thanks Gary's Student, was trying to figure out how to format this thing correctly when you did so. Commented Jul 27, 2014 at 16:45
  • replace ActiveWorkbook.Sheets("data").Range(Cells(1, 1), Cells(1, cnum)).Value with ActiveWorkbook.Sheets("data").Range(ActiveWorkbook.Sheets("data").Cells(1, 1), ActiveWorkbook.Sheets("data").Cells(1, cnum)).Value ? Commented Jul 27, 2014 at 16:46

3 Answers 3

1

Try this version:

Sub DCList_sub()
    Dim DCList
    Dim cnum As Long

    ' count the number of columns that have been used in the workbook
    cnum = ActiveWorkbook.Sheets("data").UsedRange.Columns.Count

    ' set array dimensions to 1 row & Cnum columns
    ReDim DCList(1 To 1, 1 To cnum)

    ' set array values to values found in the range (A1, cnum 1)
    DCList = ActiveWorkbook.Sheets("data").Range(Cells(1, 1), Cells(1, cnum)).Value

    'Other info:
    ' DCList is a global variable set as Variant type
    ' The overarching goal of this sub is purely to determine the column names of
    ' a dataset so that (in another sub) the user can select which column to lookup

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

3 Comments

It works! Now the question: why did this one work and my original code did not? Is it because I had the dclist array redim with only 1 to cnum?
1 I changed the name of the sub to be different from the name of the array. 2 I changed Integer into Long. I ReDimmed DCList as indicated.
in fact it shouldn't work, exept if activesheet is already sheets("Data").
0

If you want DCList to contain the first row of the worksheet, there's no need to redim DClist. Simply:

Dim DCList As Variant
DCList = ActiveWorkbook.Sheets("Data").UsedRange.Rows(1)

should do that.

If you need the number of columns for some other reason

cnum = ubound(DCList,2)

Comments

0

Gary's student code corrected:

Sub DCList_sub()
Dim DCList() as variant
Dim cnum As Long


with ActiveWorkbook.Sheets("data") 'might be better with thisworkbook, if the code is in the same workbook...    

    ' count the number of columns that have been used in the workbook
    cnum = .UsedRange.Columns.Count 

    ' set array dimensions to 1 row & Cnum columns
    ReDim DCList(1 To 1, 1 To cnum)

    ' set array values to values found in the range (A1, cnum 1)
    DCList = .Range(.Cells(1, 1), .Cells(1, cnum)).Value 'note the two "." before "Cells"

end with

'do stuff here with the array...

'free memory Erase DClist

End Sub

Comments

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.