为什么这段代码只将变量写入表的第一行?

时间:2017-07-03 18:27:44

标签: excel vba

为什么这段代码只在第11行而不是第12和第13行写入变量“Duracao”(持续时间)?您可以在此处查看Excel表格的图像:https://i.stack.imgur.com/Z6LJw.png

基本上,我为每架直升机准备了几个有开始和结束日期的工作,我希望得到每份工作的持续时间(以天为单位)。

 Sub EscalaDinâmicaHTA()

Dim NumHelis As Integer
Dim DataInicio 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

NumHelis = Worksheets(1).Range("B8").Value
LinHeliInicial = 11
LinTotal = LinHeliInicial + NumHelis - 1

i = 2
j = 3
k = 4


Do While LinHeliInicial <= LinTotal
Do While i < 26 And j < 27 And k < 28

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

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

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

ContData = ContData - 43072        
Duracao = Duracao - 43072

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

Loop

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



LinHeliInicial = LinHeliInicial + 1
Loop



End Sub

1 个答案:

答案 0 :(得分:0)

  i = 2
  j = 3
  k = 4
  Do While LinHeliInicial <= LinTotal
  Do While i < 26 And j < 27 And k < 28

您需要在i, j, k的每次迭代中重新初始化LinHeliInicial。将上述行的顺序切换为:

  Do While LinHeliInicial <= LinTotal
    i = 2
    j = 3
    k = 4
    Do While i < 26 And j < 27 And k < 28