2017-07-04 138 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