简体   繁体   中英

VBA - Extracting data from excel to a dynamic 2D array

sort of a newbie with VBA and I was wondering how you can create a dynamic 2D array which will be the size of the selected data in excel (Not knowing how big the data is) . I am unfamiliar with the VBA syntax and most of the questions I saw dealt with static data where the person knows the size of the excel table. I have already created a function which automatically selects the data in the excel sheet. Now I wish to know how I can place all these data into a 2D Array.

Sorry if this is a common question, I am more familiar with 2D arrays with other languages, however I am getting mixed up with people using Range, Array and others in VBA

Also the data is strings in each element of the table. Please also advise how I will pull this data out :) Thank you

This code will put into array for you ..

Sub SelectionToArray()
Dim arrSelection() As String
Dim i As Integer

i = 0
ReDim arrSelection(i)

For Each c In Selection
    arrSelection(i) = c.Value
    i = i + 1
    ReDim Preserve arrSelection(i)
Next c

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM