Get Spreadsheet List into VBA Array

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
If I have a list like this:

Excel 2012
A
1Company Names
2Company A
3Company B
4Company C
5Company D
Sheet1


How would I get these into an array (Excluding the Header)? I am familiar with hard coding arrays like this:
Code:
 CompArray = Array("Company A", "Company B", "Company C", "Company D") [COLOR=#008000]'Define Name Array[/COLOR]

How can I do the same thing but make the list dynamic. So the array will consist of Company's A-D now. However, if I added for example a few more:

Excel 2012
A
1Company Names
2Company A
3Company B
4Company C
5Company D
6Company E
7Company F
8Company G
Sheet1


It would now encompass these additional Companies.... (Companies A-G)

Any help would be much appreciated. Thanks.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Code:
Sub addArray()

    Dim compNames() As Variant


    Dim rowStart As Integer
    Dim rowEnd As Long
    
    rowStart = 2
    rowEnd = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row


    compNames = Range(Cells(rowStart, 1), Cells(rowEnd, 1))


    For x = 1 To rowEnd - 1
        MsgBox compNames(x, 1)
        ' THIS IS JUST TO SHOW AND TEST
    Next x




End Sub
 
Upvote 0
NeonRedSharpie/Andrew Poulsom,

Thanks so much. I appreciate the help. This will come in handy for many tasks. :)
 
Upvote 0
Make sure you note that the array is now 2 dimensional so you have to use comparray(x, 1) when referring to elements of it, not just comparray(x)
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top