【发布时间】:2016-10-04 05:08:30
【问题描述】:
我在两个包含日期的单元格之间做一个简单的减法以获得期间。在 Data_p 工作表 Range ("4") 中的每个客户都将在相应列中包含所有订单日期。所以减法会在第二个日期和第一个日期之间进行,以此类推,结果将粘贴到 Data_p_mgnt 中。必须执行此函数,直到每个客户都没有更多日期为止。
我有以下代码,但我不知道为什么它在 Data_p 中找到空单元格时它不会停止。任何见解将不胜感激。
Sub Prueba_Data_p_mgnt()
Sheets("Data_p_mgnt").Select
Range("B5").Select 'Starts in cell B5
Do Until IsEmpty(Worksheets("Data_p").Range("B5")) 'Checks if cells in Data_p are Empty or Blank
ActiveCell.FormulaR1C1 = "=Data_p!R[1]C-Data_p!RC" 'Makes the subtraction between cells
ActiveCell.Offset(1, 0).Range("A1").Select 'Moves down for paste the next period
Loop 'Loop until there's an Empty cell in Data_p
'Then should move to next client to the right and repeat until there are no more clients in row 4
End Sub
【问题讨论】:
-
你总是在测试 Range("B5")。您没有将 B5 更改为空白,那么它将永远循环。
-
除了
Range("B5"),你甚至没有测试任何东西。 -
@ScottCraner 有它 - 通读 how to avoid using
.Select/.Activate并应用你学到的东西,这应该可以解决问题。要保持代码不变,您需要Do Until IsEmpty(ActiveCell)...但不要这样做,请使用范围。只需查看如何在单元格/范围内动态循环。 -
@ScottCraner,我怎样才能让它在每个循环中向下偏移?
-
另外,
ActiveCell.Offset(1, 0).Range("A1")中的.Range("A1")是完全多余的。