Excel VBA反向循环超越最终值

我正在debugging下面的代码。 当我循环它时,我注意到row_j的值为1,尽pipeFor row_j = LastRow_date_new To 2 Step -1

我希望row_j的最小值为2,因为行1中没有数据row_j值是20/01/2015 09:15:00格式的date,没有20/01/2015 09:15:00或空值。

row_j用于设置传递给Copy_to_b并在那里传播错误。

任何人都可以看到我的问题来自哪里?

另外,你可以推荐一个方法来退出循环时,所需的值匹配Date_end满足

谢谢

 Sub select_date_range(LastCol As Long, LastRow_date_new As Long, DateMax As Date, Date_end As Date) Dim SearchCol As Integer Dim row_i As Integer Dim row_j As Integer Dim Start_row As Integer Dim End_row As Integer With Worksheets("a") For SearchCol = 1 To LastCol Step 3 LastRow_date_new = Application.CountA(.Range((.Cells(1, SearchCol)), (.Cells(65536, SearchCol)))) For row_i = 2 To LastRow_date_new If Sheets("a").Cells(row_i, SearchCol).Value = DateMax Then Start_row = row_i Next row_i For row_j = LastRow_date_new To 2 Step -1 If Sheets("a").Cells(row_j, SearchCol).Value = Date_end Then End_row = row_j Next row_j ''''''' use range col1, row i to col2, row j to copy into new sheet Call copy_to_b(Start_row, SearchCol, End_row) Next SearchCol End With End Sub 

For row_j = LastRow_date_new To 2 Step -1 ,并在每个For ,你将有你的variables等于上一个值+步骤,这是如何退出本身。

所以如果你不想把这个variables设置为1,你必须在你的For -loop之后将它设置为正确的值,如下所示:

  For row_i = 2 To LastRow_date_new If Sheets("a").Cells(row_i, SearchCol).Value = DateMax Then Start_row = row_i Next row_i row_i=2 For row_j = LastRow_date_new To 2 Step -1 If Sheets("a").Cells(row_j, SearchCol).Value = Date_end Then End_row = row_j Next row_j row_j=2 

当你的标准匹配时退出循环,你可以使用Exit For ,有些人会说这不是优雅的,但它确实有效(和下面的代码审查另一种方式)

 Sub select_date_range(LastCol As Long, LastRow_date_new As Long, DateMax As Date, Date_end As Date) Dim SearchCol As Integer Dim row_i As Integer Dim row_j As Integer Dim Start_row As Integer Dim End_row As Integer With Worksheets("a") For SearchCol = 1 To LastCol Step 3 LastRow_date_new = Application.CountA(.Range((.Cells(1, SearchCol)), (.Cells(65536, SearchCol)))) For row_i = 2 To LastRow_date_new If CDate(.Cells(row_i, SearchCol).Value) <> CDate(DateMax) Then Else Start_row = row_i Exit For End If Next row_i For row_j = LastRow_date_new To 2 Step -1 If CDate(.Cells(row_j, SearchCol).Value) <> CDate(Date_end) Then Else End_row = row_j Exit For End If Next row_j ''''''' use range col1, row i to col2, row j to copy into new sheet Call copy_to_b(Start_row, SearchCol, End_row) Next SearchCol End With End Sub 

要replace您的For ,您可以使用Do While When ou Do Until

 row_j = LastRow_date_new Do While row_j >= 2 And .Cells(row_j, SearchCol).Value <> Date_end row_j = row_j - 1 Loop End_row = row_j 

我相信您的问题可能是由于您所查看的列中缺less任何值或只有一个值而发生的。 另外,如果你在这个列中有空值,你可能会错过值,如果是这样的话,我会改变我的答案。 你应该改变

 With Worksheets("a") For SearchCol = 1 To LastCol Step 3 LastRow_date_new = Application.CountA(.Range((.Cells(1, SearchCol)), (.Cells(65536, SearchCol)))) For row_i = 2 To LastRow_date_new If Sheets("a").Cells(row_i, SearchCol).Value = DateMax Then Start_row = row_i Next row_i For row_j = LastRow_date_new To 2 Step -1 If Sheets("a").Cells(row_j, SearchCol).Value = Date_end Then End_row = row_j Next row_j ''''''' use range col1, row i to col2, row j to copy into new sheet Call copy_to_b(Start_row, SearchCol, End_row) Next SearchCol End With 

对此

 Dim xlws As Excel.Worksheet Dim lngrow as Long Set xlws = Thisworkbook.Sheets("a") For SearchCol = 1 to LastCol Step 3 While xlws.Range.Cells(lngrow,SearchCol).Value <> "" If xlws.Cells(lngrow,SearchCol).Value = DateMax Then Start_row = lngrow Elseif xlws.Cells(lngrow,SearchCol).Value = Date_end Then End_row = lngrow End If lngrow = lngrow + 1 Loop Call copy_to_b(Start_row, SearchCol, End_row) Next SearchCol