2013-11-28 77 views
0

我想創建一個函數,它執行一些檢查並向我的調用子函數返回一個數組。這個數組不能真的在sub中創建,然後傳遞給函數進行操作。子只是調用必須創建一個數組並將其返回給子的函數。這可能嗎?有一個函數返回一個數組

這是我的功能。第一行顯然是不正確的,但這是我想要做的想法。

Function rMMopened(1 to 1000)() As Long 

Dim r As Long, i As Long 
Dim ttd As Worksheet 
Dim lrttd As Long 

Set ttd = Sheets("Tasks_to_do") 

lrttd = ttd.Cells(Rows.count, 1).End(xlUp).Row 
ReDim rMMopened(1 To lrttd) 

i = 1  
For r = 2 To lrttd  
    If ttd.Cells(r, 10) = "Phase 2" Then   
     If InStr("MASTER", ttd.Cells(r, 4)) Then    
      rMMopened(i) = r 
      i = i = 1     
     End If   
    End If   
Next 

ReDim Preserve rMMopened(1 To (i - 1))  
End Function 

感謝

+0

爲什麼不能在此數組中的'Sub'產生的呢? –

+0

好吧,它「可以」,但它會更清潔這種方式。我猜這可能無法完成,或者它更容易創建子數組中的數組? – user2385809

回答

2

如何:

Function rMMopened() As Long()  ' *** changed 

Dim Values() As Long    ' *** added 
Dim r As Long, i As Long 
Dim ttd As Worksheet 
Dim lrttd As Long 

Set ttd = Sheets("Tasks_to_do") 

lrttd = ttd.Cells(Rows.Count, 1).End(xlUp).Row 
ReDim Values(1 To lrttd)   ' *** changed 

i = 1 

For r = 2 To lrttd 

    If ttd.Cells(r, 10) = "Phase 2" Then 

     If InStr("MASTER", ttd.Cells(r, 4)) Then 

      Values(i) = r   ' *** changed 
      i = i = 1 

     End If 

    End If 

Next 

ReDim Preserve Values(1 To (i - 1)) ' *** changed 
rMMopened = Values     ' *** added 

End Function