在Excel VBA中构build数组
期望的结果:结果将是一个函数,它从B列构build一个值的数组。通过在列A中具有相同的值来限制值。'例如,列A值= 1 myArray =(0,1,2)'' B列值= 2 myArray =(4,5,6,7,8)'
第一次尝试在VBA中使用数组,需要帮助。 我在Excel中的列A和B中有以下数据:
AB 1 0 1 1 1 2 2 4 2 5 2 6 2 7 2 8 3 9 3 10 3 11 4 12 4 15 4 18
我有以下的VBA代码:
Function buildMyArray() Dim ARange as Range Dim B as Integer Dim myArrary as Variant For Each ARange In Sheets("SheetName").Range("B:B") If ARange.Value = 1 Then B = Application.WorksheetFunction.VLookup(ARange.Value, Sheets("SheetName").Range("A:B"), 2, False) myArray = Array(B) End If Next ARange End Function
我试图build立一个函数,将search列A每个整数的实例(我将最终dynamic传递)。 然后,对于该整数的每个实例,在相邻的列(B)上执行一个查找。 然后我想从查找中创build一个数组(在上面的情况下(0,1,2))。
上面的代码是我得到的最接近的代码。 B(0)中的第一个值是数组中唯一的值。 我有理由相信这是因为数组逻辑在If语句中。 但是,如果我把它移出那里,我得到编译错误。
请帮助新手学习。 谢谢。
把一个数组看作是Excel表格的一行,列是索引。 你想要做的是你说的是存储在数组中的值。 所以你得到你的价值,B,你想把它放在数组中。
你想做什么:
当声明数组时,声明它像Dim myArrary() as Variant
注意括号(如果它们是必需的,但是它的最佳实践不是肯定的。
声明一个空数组。 像没有列的Excel表一样。 如果你知道的事实数组将有5个值,你可以声明:
Dim myArrary(1 to 5) as Variant
说,它是一个有5个索引的数组。 (如5列的excel行)
数字,1到5,这些是开始和结束索引号码。 所以,在这个数组中,如果想重新find第一个地方,你可以把myArray(1)
的方括号括起来,就像你引用数组中的特定索引一样。 第二点是myArray(2)第三个myArray(3)第四个myArray(4)和第五个(最后一个点)将是myArray(5)。
你可以声明像dim myarray(0到5)作为变体,这意味着第一个索引将是myArray(0) – 这个数组将有6个点 – 0,1,2,3,4,5
所以,在你的代码中,你需要一个计数器来跟踪你所在循环的迭代次数,并在循环底部增加counter = cojunter + 1
。 (它会增加每次循环运行)然后,用myArray(counter) = B
replacemyArray = Array(B)
所以每次循环运行时, B
都会进入arrays中的一个点。
还有一件事
因为我们不知道数组有多less个点,所以我们声明为空: dim myArray() as variant
你必须在有空格之前重新定义它,然后才能添加任何东西。 所以,在循环开始时,你Redim Preserve MyArray(1 to counter)
这个重新定义你的数组从1到任何你的计数器。 由于计数器每次增加1次,每次将有1个空间添加到arrays中。
所以基本上,
将Dim myArrary as Variant
replaceDim myArrary as Variant
Dim myArArray()作为variables添加: dim counter as long
variables(命名它,无论你想要的,只是一致)
在循环之前添加counter = 1
来初始化计数器
在循环中添加ReDim myArray(1 to counter)
,在if语句中(因此只有在findB时才添加空格
添加counter = counter + 1
,如果结束,如果,但又在里面,因此只有增加,如果B的值被发现
并在if语句中将myArray = Array(B)
更改为MyArray(counter) = B
这是我创build的一个样本。 尝试这个
Option Explicit Sub sample() Dim ws As Worksheet Dim rng As Range, aCell As Range, bCell As Range Dim MyAr() As String Dim n As Long Dim SearchString '~~> Set this to the relevant worksheet Set ws = ThisWorkbook.Sheets("Sheet1") SearchString = 2 With ws '~~> Set this to your range Set rng = .Range("A1:A14") Set aCell = rng.Find(What:=SearchString, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then Set bCell = aCell '~~> Store the value from Col B in the array ReDim Preserve MyAr(n) MyAr(n) = aCell.Offset(, 1).Value n = n + 1 '~~> Find Next occurance Do Set aCell = rng.FindNext(After:=aCell) If Not aCell Is Nothing Then If aCell.Address = bCell.Address Then Exit Do '~~> Store the value from Col B in the array ReDim Preserve MyAr(n) MyAr(n) = aCell.Offset(, 1).Value n = n + 1 Else Exit Do End If Loop End If End With '~~> This will give you the results For n = LBound(MyAr) To UBound(MyAr) Debug.Print MyAr(n) Next n End Sub
截图 :
输出:
在VBA中,没有简单的“将项目添加到数组”的概念。 你总是必须事先声明你想要的数组有多大,那么你可以使用数组中的项目。 尽pipe可以编写增加数组大小的代码,但这通常不是正确的方法。
在这种情况下,构build数组将是一个两步的过程:首先需要计算将要放入数组中的项数,然后您需要填充数组。 在我的示例中, x
是您在A列中search的数字。
Function MakeArray(x As Integer) As Variant Dim result() As Variant Dim result_count As Long Dim i As Long Dim cursor As Range result_count = WorksheetFunction.CountIf(Range("A:A"), x) ' size the array to how many elements we need, minus one ' because the array indexes start at zero ReDim result(result_count - 1) ' scan through the data and populate our array Set cursor = Range("A2:B2") i = 0 Do Until IsEmpty(cursor(1)) If cursor(1) = x Then result(i) = cursor(2) i = i + 1 If i > UBound(result) Then Exit Do End If Set cursor = cursor.Offset(1) Loop MakeArray = result End Function
可能不算是一个答案,但没有VBA,在C2和复制下来,以适应:
=IF(A3<>A2,IF(A1=A2,C1&","&B2,"("&B2)&")",IF(A1=A2,C1&","&B2,"("&B2))
然后将ColumnC和Paste Special Values复制到顶部,filterselect不包含)
并删除可见应显示:
列开始范围
ColumnB结束范围
ColumnC数组