2017-03-02 10 views
1

1か月に1時間ごとの統計を含む一連のデータがあります。範囲を検索して範囲に行がないかどうかを確認する方法

時々、データに行がありません(たとえば、データベースが保守のために停止しているため、統計情報を収集できません)。私がしなければならないことは、1時間ごとに1時間ずつ増加するかどうかを各24時間ブロックで判断することです。残念ながら

Sub btnAddZero() 
    Dim srcRange As Range 
    Dim intOffset As Integer 

    'select the starting row for counting the hours 
    Set srcRange = Sheets("Raw Data").Range("A4") 
    'Set the beginning offset to 4 to account for empty 4 rows 
    intOffset = 4 

    'number of days in the month 
    For j = 0 To 30 
     'number of hours in a day 
     ' rows 

     Dim i As Integer 

     For i = 0 To 23 


       'if the first cell is not 1 more than the cell below it 
       If srcRange.Cells(intOffset, 4) <> srcRange.Cells(intOffset + i, 4) Then 
        'shift everything below this line down 
        srcRange.Rows(intOffset + i).Insert shift:=xlShiftDown 
        'set everything in this row to be 0 
        srcRange.Cells(intOffset + i, 1).Value = 0 
        srcRange.Cells(intOffset + i, 2).Value = 0 
        'set the value to be the same value as the cell above 
        srcRange.Cells(intOffset + i, 3).Value = srcRange.Cells(Row - 1, 3).Value 
        srcRange.Cells(intOffset + i, 4).Value = 0 
        srcRange.Cells(intOffset + i, 5).Value = 0 
       End If 

     Next i 
     intOffset = intOffset + i 
     'off set the range for next 24 hours 
     Set srcRange = srcRange.offset(intOffset) 
    Next j 

End Sub 

:時間の列の値が2ずつ増加した場合、私はこれは私がしようとしたものです0

で細胞のそれぞれを空白行を追加し、移入する必要があります...これは0に巨大な行の範囲を設定します(私は22〜28行の0を挿入していると思います)。内側のfor-loopの4回目の反復の後、外側のfor-loopを通して最初に、これは0の行が始まるところです。

私の最初の最大の問題は、欠落しているかどうかを確認するためのチェックが正しく機能していないことです。

ご協力いただければ幸いです!

私が使用しているデータはわずかですが、値15が4番目の列にないことがわかります。私は

9292 12377 2017年1月30日12 2471

11195 15028 2017年1月30日13 1392を含む行の後に0の行を追加したいです2017- 01-30 14 374

1911 1959 2017年1月30日16 8

11995 13181 2017年1月30日17 181

+0

どのようなタイプの値が列4(おそらくタイムスタンプ)に入っていますか?それは日時ですか?それは常に正確に時間になるのだろうか? – PeterT

+0

これは、1日の時間によって0〜23の整数です – Sarah

答えて

1

私はあなたが不足している時間を検出しているかのロジックを考えます行を挿入するのは間違いです。問題を少し違った形で見て、私は例として以下のロジックを思いついた。うまくいけば、それはあなたを解決に近づけるものです。

Option Explicit 

Sub MissingHourCheck() 
    Dim wb As Workbook 
    Dim ws As Worksheet 
    Set wb = ThisWorkbook 
    Set ws = wb.Sheets("Raw Data") 

    Dim timeSlot As Range 
    Dim nextTimeSlot As Range 
    Set timeSlot = ws.Range("D5") 
    Set nextTimeSlot = timeSlot.Offset(1, 0) 

    Dim expectedHour As Long 
    expectedHour = 0 

    Do While Not IsEmpty(nextTimeSlot.Value) 
     If expectedHour <> timeSlot.Value Then 
      '--- oops, we're missing at least one hour. 
      ' how many hours (rows) do we need to insert? 
      Dim missingHours As Long 
      If timeSlot.Value > expectedHour Then 
       '--- it's the middle of the day, our math is easy 
       missingHours = timeSlot.Value - expectedHour 
      Else 
       missingHours = 23 - expectedHour + 1 
      End If 
      timeSlot.Resize(missingHours).EntireRow.Insert Shift:=xlShiftDown 
      expectedHour = expectedHour + missingHours 
     Else 
      Set timeSlot = timeSlot.Offset(1, 0) 
      Set nextTimeSlot = timeSlot.Offset(1, 0) 
      expectedHour = expectedHour + 1 
     End If 
     If expectedHour >= 24 Then 
      expectedHour = 0 
     End If 
    Loop 
End Sub 
+0

これは私が必要とするものに非常に近いです!あなたの助けをもう一度ありがとう! – Sarah

関連する問題