2016-10-03 9 views
0

私は期間を得るために日付を含む2つのセルの間で簡単な減算をしています。 Data_pワークシートのRange( "4")内のすべてのクライアントは、それぞれの列にすべての受注日を持ちます。したがって、減算は2番目の日付と1番目の日付の間で行われ、結果はData_p_mgntに貼り付けられます。この関数は、クライアントごとに日付がなくなるまで実行する必要があります。IsEmptyが機能しなくなるまで、アイデアはありますか?

私は以下のコードを持っていますが、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 
+3

あなたは常に範囲(「B5」)をテストしています。 B5を空白に変更していない場合、永遠にループします。 –

+0

あなたは 'Range(" B5 ")以外のものをテストしていません。 – Comintern

+2

@ScottCranerには、[.Select'/'.Activate'の使用を避ける方法]があります。(http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel- vba-macros)を使用して学習した内容を適用すると、問題が取り除かれます。あなたのコードをそのまま保つには、Do Is until IsEmpty(ActiveCell)をしたいのですが、そうしないで、範囲を使用してください。セル/範囲を動的にループする方法を調べるだけです。 – BruceWayne

答えて

2

私は、これはあなたがやろうとしているものであると信じて:

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 
+0

はい!ありがとうございました! –

+0

@AndréMoraBarreira - 私はあなたのロジックが各列の最後のセル(次の空のセルから最後の日付を引くことになります)でうまくいくとは思わないので、私はその答えを編集して、 。 – YowE3K

+0

はい、それは完璧です、私はそれらの数字を削除するためにIFを使用する予定でしたが、あなたが提案したものは良いです –

1

これは、私は願って、トリックを行う必要があります。

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を行う上で計画しているかわかりません。

+0

私が使用しているデータはCell5からData_pにあります。 B6-B5、B7-B6、B8-B7は、空白のセルがなくなるまで減算されます。この減算の結果は、セルB5を参照してData_p_mgntにリストされます。 –

関連する問題