直到IsEmpty不工作,有什么想法?

我在两个包含date的单元格之间做一个简单的减法以获得周期。 Data_p工作表中的每个客户端范围(“4”)将具有相应列中的所有订单date。 因此,减法将在第二个date和第一个date之间,依此类推,并将结果粘贴到Data_p_mgnt中。 这个函数必须被执行,直到每个客户端没有更多的date。

我有下面的代码,但我不知道为什么它不会停止,当它发现和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 

我相信这是你正在做的事情:

 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 

我希望这样做可以做到这一点:

 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做什么。