【问题标题】:Do Until IsEmpty not working, any ideas?直到 IsEmpty 不起作用,有什么想法吗?
【发布时间】: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") 是完全多余的。

标签: vba excel


【解决方案1】:

我相信这就是你想要做的:

Sub Prueba_Data_p_mgnt()
    Dim wsMgnt As Worksheet
    Dim wsData As Worksheet
    Dim rowNo As Long
    Dim colNo As Long
    Set wsMgnt = Worksheets("Data_p_mgnt")
    Set wsData = Worksheets("Data_p")

    colNo = 2
    Do Until IsEmpty(wsData.Cells(4, colNo)) 'Checks if cells in Data_p are Empty or Blank

        rowNo = 5
        Do Until IsEmpty(wsData.Cells(rowNo, colNo)) 'Checks if cells in Data_p are Empty or Blank
        'Alternatively, to avoid subtracting the last non-blank cell from a blank cell
        'Do Until IsEmpty(wsData.Cells(rowNo + 1, colNo)) 'Checks if cells in Data_p are Empty or Blank

            wsMgnt.Cells(rowNo, colNo).FormulaR1C1 = "=Data_p!R[1]C-Data_p!RC" 'Makes the subtraction between cells
            'Alternatively, if you would rather have values than formulae
            'wsMgnt.Cells(rowNo, colNo).Value = wsData.Cells(rowNo + 1, colNo).Value - wsData.Cells(rowNo, colNo).Value

            rowNo = rowNo + 1 'Moves down for paste the next period
        Loop 'Loop until there's an Empty cell in Data_p

        colNo = colNo + 1 'Then should move to next client to the right and repeat until there are no more clients in row 4 
    Loop

End Sub

【讨论】:

  • 是的!谢谢!
  • @AndréMoraBarreira - 我认为您的逻辑不适用于每列中的最后一个单元格(您将从以下空单元格中减去最后一个日期),所以我刚刚编辑了答案提供一种替代方法来避免这种情况。
  • 是的,这很完美,我打算使用 IF 来删除这些数字,但你建议的更好
【解决方案2】:

我希望这应该可以解决问题:

Sub Prueba_Data_p_mgnt()
Dim dataWS As Worksheet
Dim rng As Range
Set dataWS = Sheets("Data_p_mgnt")

Set rng = dataWS.Range("B5") ' what's this? You never use data_p_mgnt cell B5?

For i = 5 To 100 ' Change 100 to whatever you need
    If Not IsEmpty(Worksheets("Data_p").Range("b" & i)) Then 'Checks if cells in Data_p are Empty or Blank
        Worksheets("Data_p").Range("b" & i).FormulaR1C1 = "=Data_p!R[1]C-Data_p!RC" 'Makes the subtraction between cells
    End If 'Loop until there's an Empty cell in Data_p
Next i
End Sub

但看看你的代码,我不知道你打算用Data_p_mgnt 表上的B5 做什么。

【讨论】:

  • 我正在使用的数据位于 Data_p 中,从单元格 B5 开始。减法将进行 B6-B5、B7-B6、B8-B7 直到有一个空白单元格。此减法的结果列在 Data_p_mgnt 中,盯着单元格 B5
猜你喜欢
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 2014-12-07
  • 2020-03-12
  • 1970-01-01
相关资源
最近更新 更多