简体   繁体   中英

Copy Cells values from Excel sheet into an array

我有一个Excel工作表abc.xlsm”,并且此工作表中的值在“ A1”至“ A15”中。我想复制到第10行,然后使用VBA将所有值存储在数组中。

As mentioned in the comments, please be more specific in your questions: most questions should have at least some bit of code of what you have tried. At any rate this should work, with a couple extra notions:

Sub copy()
'Declaring an array - if you know the data type you can type is as well
Dim varray As Variant
'Declaring other variables - don't need to be separeted, just for clarity
Dim i As Long, iLenghtArray As Integer, rgData As Range, rgTarget As Range

'This is to dimension your array - you have tell VBA the lenght of it, or use REDIM
ilengtharray = 10

'Setting the range reference
Set rgData = Sheet1.Range("$A$1:$J$1")

'Then set the array = to the range you set above
varray = rgData

'Then you can interate over your array like so:
For i = 1 To UBound(varray, 2)
    Debug.Print varray(1, i)
Next

'You can also directly past your array into a suitable range
'Setting destination range:
Set rgTarget = Sheet1.Range("$A$2:$J$2")
rgTarget = varray

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