2014-01-06 97 views
-1

我目前面臨如何在循環結束後確定數組大小的問題。以下是我對該特定功能的編碼。確定數組大小

Function analyse(ByVal work_date As Date, time As String, action As String, decision As Boolean, branch As String) As String 
    Dim sh As Worksheet 
    Dim att_time(6) As String 
    Dim dated(1) As Date 
    Set sh = Worksheets("shifthr") 

    lastrec = sh.Range("C" & Rows.Count).End(xlUp).Row 
    a = 1 
    For i = 2 To lastrec 
     dated(a) = work_date 
     If branch = sh.Cells(i, 1) Then 
      att_time(a) = time 
      a = a + 1 
     End If 
    Next i 
    If att_time(a) = 4 Then 

    ElseIf att_time(a) = 6 Then 

    End If 
End Function 

謝謝你們幫助的手。非常欣賞

+0

數組的大小?但是你已經聲明瞭數組的大小? –

+0

@SiddharthRout,循環完成後,我想檢測數據已經保存在數組中多少,並從那裏需要做一些分析。對不起,有任何困惑。 – user1928481

+0

我以爲是這樣:)看到我的答案。 –

回答

0

我目前面臨關於如何循環結束後確定數組的大小的問題。

您已經在dim語句中聲明瞭數組的大小。我想你想找到數組中的元素數量?如果是的話,那麼試試這個(UNTESTED)另外我想你想保持dated(a) = work_dateIF的條件?如果不是,那麼你不需要使用a來填充dated(a) = work_date。我也不認爲你需要Function這個,因爲你沒有返回任何值analyse。使用Sub

Sub analyse(ByVal work_date As Date, time As String, _ 
action As String, decision As Boolean, branch As String) 
    Dim sh As Worksheet 
    Dim att_time() As String 
    Dim dated() As Date 
    Dim a As Long 
    Dim boolCheck As Boolean 

    Set sh = Worksheets("shifthr") 

    lastrec = sh.Range("C" & sh.Rows.Count).End(xlUp).Row 

    a = 1 

    For i = 2 To lastrec 
     If branch = sh.Cells(i, 1) Then 
      ReDim Preserve dated(a) 
      ReDim Preserve att_time(a) 

      dated(a) = work_date 
      att_time(a) = time 

      a = a + 1 

      boolCheck = True 
     End If 
    Next i 

    If boolCheck = True Then 
     Debug.Print UBound(dated) 
     Debug.Print UBound(att_time) 

     'OR 

     Debug.Print a - 1 
    Else 
     Debug.Print "No Matching records found" 
    End If 
End Sub