对不起,我在上一条消息中说得不够清楚!
情况如下 用户可以在Excel数组中添加新行。 我想在宏的动态数组的最后一行中存储新参数,以便进行其他计算。
例如:我有一个包含2列的数组:参数和值 参数< - B1列 参数1 参数2 参数3
值< - C1列 VAL1 VAL2 VAL3
此后我所做的却不起作用!
Dim RowCount As Integer
RowNumber = Sheets("Feuil1").Range("C1").End(xlDown).row
'MsgBox "Rows:" & RowNumber-1
Dim tb_val() As Integer
ReDim tb_val(RowNumber - 1)
Dim lc() As Integer
For i = 1 To RowNumber
lc = PathFinder("Feuil1", Cells(i, 2).Value)
tb_val(i - 1) = Sheets("Feuil1").Cells(lc(1), lc(2) + 1).Value
Next i
P.S:PathFinder(“worksheet1”,“word1”)发送带有单元格详细信息的arr(2) - 列&在“工作表1”中找到的“word1”行
Function PathFinder(sheet1 As String, word1 As String) As Integer()
Dim rng As Range
Dim rngFound As Range
Dim temp(2) As Integer
Set rng = Sheets(sheet1).Range("A:B")
Set rngFound = rng.Find(word1, LookAt:=xlWhole, SearchOrder:=xlByRows)
If rngFound Is Nothing Then
MsgBox "not found"
Else:
temp(1) = rngFound.row
temp(2) = rngFound.column
End If
PathFinder = temp
End Function
由于
答案 0 :(得分:0)
好吧,如果我理解正确,听起来你想要这样做:
ReDim Preserve arr(2)
arr(2) = val2
ReDim
将调整数组的大小。 Preserve
保留数组中已有的值(否则将重新初始化。
答案 1 :(得分:0)
不确定我完全理解,但这是我的意见: RowNumber(我认为)错了;使用'With';第二个例子展示了如何制作一个非常快的数组。
Dim RowCount As Long 'Long is faster , and you never know how many lines you need
Dim Sh as worksheet
set Sh = thisworkbook.Sheets("Feuil1")
with Sh
RowNumber = .Range(.rows.count,"C").End(xlUp).Row ' "C" can be replaced by 3, same effect.
'MsgBox "Rows:" & RowNumber
end with
Dim tb_val() As Long
ReDim tb_val( 1 to RowNumber)
Dim lc() As Long
For i = 1 To RowNumber
lc(i) = PathFinder("Feuil1", Cells(i, 2).Value) 'no idea what this does ???
tb_val(i) = Sh.Cells(lc(1), lc(2) + 1).Value 'must be mistacke, no (i) in lc() ??
Next i
好的,我不明白你的所有代码,仍然会考虑这个:
Dim MyArray() as Variant 'must be variant for this effect
Redim MyArray (1 to RowNumber , 1 to 3) 'if only 3 columns, Rownumber is the same as above.
with sh 'same Sh as in example above
MyArray= .Range ( .cells( 1,1 ) , .cells ( rownumber,3 ) ).value 'stocks all the cells (in 3 columns in example in a VBA Array for faster calculs later
'this way any loop or calculation is way faster, using Myarray(y,x) instead of Sh.cells(y,x).value
end with
'Long Loop + calculs... for i=1 to Rownumber ....
Sh.range( "A1:C" & RowNumber).Value = MyArray 'Writes Back the VBA Array to the Worksheet (only needed if changes made inside the values of the array)
'range ("A1:C" & RowNumber) has the same effect as Range(cells(1,1) , cells(rownumber,3)) from above , just wanted to say