2017-07-04 55 views
0

如何在此代碼中查找數組DataInicio中的重複日期?我需要找到所有重複日期和次數。對我來說,知道它只是重複是沒有用的。如何查找此VBA代碼中的重複日期?

謝謝!

我一直在努力弄清楚,但沒有任何工作。

Sub EscalaDinâmicaHTA() 

Dim NumHelis As Integer 
Dim DataInicio(1 To 15) As Date 
Dim DataFim As Date 
Dim ContData As Double 
Dim LinHeliInicial As Integer 
Dim i As Integer 
Dim j As Integer 
Dim k As Integer 
Dim a As Integer 
Dim b As Integer 


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

a = 1 

Do While LinHeliInicial <= LinTotal 

i = 2 
j = 3 
k = 4 

Do While i <= 14 And j <= 15 And k <= 16  '26,27,28 

DataInicio(a) = Worksheets(1).Cells(LinHeliInicial, i).Value 
ContData = DateValue(DataInicio(a)) 
ContData = Val(ContData) 

DataFim = Worksheets(1).Cells(LinHeliInicial, j).Value 
DataFim = DateValue(DataFim) 

Duracao = DataFim - DateValue(DataInicio(a)) 
Worksheets(1).Cells(LinHeliInicial, k).Value = Duracao + 1 
Duracao = ContData + Duracao 
Duracao = Val(Duracao) 

ContData = ContData - 43072   '43072 é a descontar as colunas A,B,C,D,etc até ao começo do calendário 
Duracao = Duracao - 43072 


Do While ContData <= Duracao 
Cells(LinHeliInicial, ContData).Interior.ColorIndex = 4 
ContData = ContData + 1 
Loop 

k = k + 3 
j = j + 3 
i = i + 3 

a = a + 1 

Loop 

LinHeliInicial = LinHeliInicial + 1 

Loop 


End Sub 
+0

你能告訴我們你是怎麼都存儲在您的Excel數據? –

回答

0

這會輸出數組中的重複數目。請注意,如果它多次出現,它將多次輸出相同的元素。通過跟蹤另一個數組中的選中元素,可以很容易地解決這個問題,並且只有在之前沒有檢查過的情況下才檢查它們(例如,爲具有唯一條目的數組進行檢查)。

Dim counter As Integer 
Dim varElement1 As Variant 
Dim varElement2 As Variant 

'get element within array 
For Each varElement1 In array 
    'reset counter 
    counter = 0 
    For Each varElement2 In array 
     'compare varElement1 with each element of array 
     If varElement2 = varElement1 Then 
      'increase counter if match was found (1 will alway be found -> the element itself) 
      counter = counter + 1 
     End If 
    Next varElement2 
    'output number if more than 1 (more than itself) was found 
    If counter > 1 Then 
     Debug.Print varElement1 & " occured " & counter " times" 
    End If 
Next varElement1 

編輯:簡化代碼,添加註釋

+0

我應該如何實現它?我把它放在最後一個循環之後,並將數組更改爲DataInicio,但它什麼都不做。 – MiguelLeal

+0

您是否檢查[立即窗口](https://www.excelcampus.com/vba/vba-immediate-window-excel/)輸出?你是否實例化(暗淡)所有變量? –

+0

是的......我現在有運行時錯誤1004,因爲我退出了「接下來的錯誤恢復」 – MiguelLeal