简体   繁体   English

如何创建Excel列内容的数组

[英]How do I create an array of the contents of a excel column

I have 10 values in column A of a excel spreadsheet (it will be more) is there a way to take the values of the column and put them into an array? 我在Excel电子表格的A列中有10个值(将会更多),是否可以将列的值放入数组中?

And if possible would it be possible to put the values in a different order than they are in in the spreadsheet. 并且如果可能的话,可以将值以与电子表格中不同的顺序放置。 For example, if my spreadsheet values are "Apple" "Orange" and "Banana", then I would like my array to look something like, position 0 "Orange" position 1 "Banana" and position 2 "Apple". 例如,如果我的电子表格值是“ Apple”,“ Orange”和“ Banana”,那么我希望数组看起来像是位置0“ Orange”,位置1“ Banana”和位置2“ Apple”。

Does anybody know how this might be done? 有人知道怎么做吗? By the way, it needs to be scalable from 10 to 1000 values without editing the code much 顺便说一下,它需要从10个值扩展到1000个值,而无需太多编辑代码

You can create an indexed array for a single column without looping as follows 您可以为单列创建索引数组,而无需循环,如下所示

Sub GetArray()
Dim X
Dim lngCol As Long
lngCol = Cells(Rows.Count, "A").End(xlUp).Row
X = Application.Transpose(Application.Evaluate("If(row(A1:A" & lngCol & "),row(1:" & lngCol & ")-1 & A1:a" & lngCol & ",0)"))
End Sub

You didn't post how you wanted to sort the data? 您没有发布想要对数据进行排序的方式吗? 在此处输入图片说明 Udpated for random ordering 适用于随机排序

Sub GetArray2()
Dim X()
Dim lngCol As Long
lngCol = Cells(Rows.Count, "A").End(xlUp).Row
X = Application.Transpose(Range("A1:A" & lngCol))
Call ShuffleArrayInPlace(X())
End Sub

The next sub uses a modified version of Chip Pearson's ShuffleArray 下一个子项使用Chip Pearson的ShuffleArray的修改版本

Sub ShuffleArrayInPlace(InArray() As Variant)
'http://www.cpearson.com/excel/ShuffleArray.aspx
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArrayInPlace
' This shuffles InArray to random order, randomized in place.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim N As Long
    Dim Temp As Variant
    Dim J As Long

    Randomize
    For N = LBound(InArray) To UBound(InArray)
        J = CLng(((UBound(InArray) - N) * Rnd) + N)
        If N <> J Then
            Temp = InArray(N)
            InArray(N) = InArray(J)
            InArray(J) = Temp
        End If
    Next N
      For N = LBound(InArray) To UBound(InArray)
      InArray(N) = N - 1 & " " & InArray(N)
      Debug.Print InArray(N)
      Next N
End Sub

在此处输入图片说明

A way to read in an entire range into an array: 一种将整个范围读入数组的方法:

Sub readText()

    Dim i As Integer
    Dim dataStr As String
    Dim arr As Variant

    'read data
    arr = Range("A1:A10").Value

    'display the data...
    For i = 1 To UBound(arr)
        'add the value at this point - given by arr(i,1) - to the string. You can access these elements 
        'directly via this sort of array notation
        dataStr = dataStr & arr(i, 1) & vbCrLf

    Next i
    'show what was in those cells
    MsgBox (dataStr)
    MsgBox (arr(3,1) )
End Sub

It is almost assuredly easier to sort in Excel first (ie alphabetical? increasing? by order? etc) rather than doing so in vba. 几乎可以肯定,首先在Excel中进行排序(即按字母顺序?按顺序增加?)比在vba中进行排序要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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