我想根据工作表中的数据创建一个2D变体数组。问题是我要使用的列不相邻,元素数量超过65,536。 (使用 let list= [];
users.forEach( function (user)
{
list.push({user: user});
});
moderators.forEach( function (moderator)
{
list.push({moderator: moderator});
});
作为Application.Index()
的{{1}}的明显限制。我该如何处理?
我对这个问题的答案对我有用。我已经阅读了很多关于这个主题的内容,我很好奇其他人是否有任何其他方法取得了重大成功,因为他们都在那里。
答案 0 :(得分:0)
Application.Index([Range/Array],[Row],[Column])
是一个非常有用的功能。
Dim arr as Variant 'This is the array you want to create.
Dim varRows as Variant 'This will hold the rows want to index
Dim LastRow as Long 'This is the last row of data you are needing
LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 'set the last row
varRows = Evaluate("row(1:" & LastRow & ")") 'Evaluate is the make or break here. You can modify your start row as well by changing the 1.
arr = Application.Index(Sheets("Sheet1").Cells, varRows, Array(1, 3, 5)) 'You can set your array to be any columns you want.
'arr will now be a 2D array arr(1 to varRows, 1 to 3) in this example.
这种方法非常快,很愚蠢。它击败了For Loop的裤子 我在这个例子中传递了一个Range,但是如果你需要将一个或多个列切成一个新数组,也可以传递一个数组
这会创建一个Variant数组,并且变体很容易获取并返回到工作表。
Dim arr as Variant 'This is the array you want to create.
Dim SmallerArr as Variant 'the new array
Dim varRows as Variant 'This will hold the rows want to index
Dim LastRow as Long 'This is the last row of data you are needing
LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 'set the last row
varRows = Evaluate("row(1:" & LastRow & ")") 'Evaluate is the make or break here. You can modify your start row as well by changing the 1.
arr = Application.Index(Sheets("Sheet1").Cells, varRows, Array(1, 3, 5)) 'You can set your array to be any columns you want.
'read a single column into a new variant array
SmallerArr = Application.Index(arr, varRows, 1) 'You can set your array to be any columns you want.
'read multiple columns into a new variant array
SamllerArr = Application.Index(arr, varRows, Array(1, 2)) 'The columns must exist in arr or you will get an error.
Dim lngArr(1 To 100000, 1 To 3) As Long 'create 2D array of type long
'Fill array
For h = 1 To 3
For j = 1 To 100000
lngArr(j, h) = j * h
Next j
Next h
SamllerArr = Application.Index(lngArr, varRows, Array(1, 2)) 'The columns must
'we just turned a long array into a variant array!
'we can read it to the sheet without a For loop!
Dim arr as Variant 'This is the array you want to create.
Dim SmallerArr as Variant 'the new array
Dim varRows as Variant 'This will hold the rows want to index
Dim LastRow as Long 'This is the last row of data you are needing
LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 'set the last row
varRows = Evaluate("row(1:" & LastRow & ")") 'Evaluate is the make or break here. You can modify your start row as well by changing the 1.
arr = Application.Index(Sheets("Sheet1").Cells, varRows, Array(1, 3, 5)) 'You can set your array to be any columns you want.
'Read 1 column to the sheet
Sheets("Sheet2").Range("A1:A" & LastRow) = Application.Index(arr, varRows, 1) ' cange the range and column accordingly
'Read multiple columns to the sheet
Sheets("Sheet2").Range("A1:B" & LastRow) = Application.Index(arr, varRows, Array(1, 2))
1)varRows
可以修改为接受任何元素位置。
2)至少在Excel 2013中,当使用Index()
而不将数组作为行/列的参数传递时,似乎没有任何行限制。
3A)以这种方式创建的所有阵列都是2D。即使它们是1列,它们仍然是1列。
3B)Application.Transpose()
也有行限制,以防万一你正在尝试...
答案 1 :(得分:0)
我不太清楚如何解释这个问题,但这里有一个方法(两种变体)做我猜的要求。这可以根据需要进行扩展/自动化/调整。
Sub ForJoshua1()
' Say, you want 2 columns: "B:B", "H:H"
Dim vX(1 To 2) As Variant
vX(1) = Range("B:B")
vX(2) = Range("H:H")
End Sub
Sub ForJoshua2()
' Say, you want 2 columns: 2, 8
Dim vX(1 To 2) As Variant
vX(1) = Sheet1.Columns(2)
vX(2) = Sheet1.Columns(8)
End Sub
例如,如果单元格“B12”中有“a”,那么
vX(1)(12,1)
等于“a”