2016-03-10 74 views
0

我有一段代碼可以在我的開始(J2)和結束日期(K2)之間創建缺失的日期。我有一個約6000人的開始和結束日期的列表,並希望重複每行的代碼。試圖創建一個循環找到缺失的日期

有誰知道我可以循環這個嗎?

Sub FindMissingDates() 

Dim FirstDate As Date 
Dim LastDate As Date 
Dim NextDate As Date 
Dim DateOffset As Range 
Dim DateIter As Date 

FirstDate = Range("J2").Value 
LastDate = Range("K2").Value 
Set DateOffset = Range("M2") 

For DateIter = FirstDate To LastDate 
    DateOffset.Value = DateIter 
    Set DateOffset = DateOffset.Offset(0, 1) 
Next DateIter 

End Sub 

回答

1

假設你的名字的名單在列JK的開始和結束日期,然後迅速改變了代碼類似下面,應該做的伎倆

Sub FindMissingDates() 
    Dim r As Long 
    Dim FirstDate As Date 
    Dim LastDate As Date 
    Dim NextDate As Date 
    Dim DateOffset As Range 
    Dim DateIter As Date 

    For r = 2 To ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Row '<~~ Loop each row 
     FirstDate = ActiveSheet.Cells(r, "J").Value 
     LastDate = ActiveSheet.Cells(r, "K").Value 
     Set DateOffset = ActiveSheet.Cells(r, "M") 

     For DateIter = FirstDate To LastDate 
      DateOffset.Value = DateIter 
      Set DateOffset = DateOffset.Offset(0, 1) 
     Next DateIter 
    Next r 

End Sub 
+0

是的,這工作完美,謝謝! – JBlack

0

你可以添加另一個For循環。

Dim sht As Wokrsheet 
Dim LastRow As Integer 
Dim FirstDate As Date 
Dim LastDate As Date 
Dim NextDate As Date 
Dim DateOffset As Range 
Dim DateIter As Date 
Dim i As Integer 

Set sht = ThisWorkbook.Worksheets("Name of your sheet") 
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 

FirstDate = Range("J2").Value 
LastDate = Range("K2").Value 
Set DateOffset = Range("M2") 

For i = First row of data To LastRow - 1 
    For DateIter = FirstDate To LastDate 
     DateOffset.Value = DateIter 
     Set DateOffset = DateOffset.Offset(i, 1) 
    Next DateIter 
Next i 

看看這是否適合你?