2014-04-01 15 views
0

对不起,我以前的消息不够清楚!识别不同的动态数组VBA Excel

这是情况 用户可以在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 

PS:路径查找器( “worksheet1”, “字词1”)发送ARR(2)用细胞细节-column &行级别的 “字词1” 中的 “worksheet1”

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 

由于

发现
+1

我真的不能说,我明白你的问题完全,但似乎你可能能够使用动态r anges(http://support.microsoft.com/kb/830287)和worksheet_change事件来完成您要做的事情......否则,请更新您的问题并更好地解释您的情况... –

回答

0

嗯,如果我理解正确的话,这听起来像你想这样做:

ReDim Preserve arr(2) 
arr(2) = val2 

ReDim将调整数组大小。该Preserve保持与已是阵列(否则会被重新初始化中的值

0

不知道我完全理解,但这里是我的意见: ROWNUMBER是(我认为)错误的;使用“与”;在第二个例子说明如何使一个非常快的阵列

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 
+0

I刚刚添加了有关路径查找器功能的详细信息... – ThinkMAL