使用xlUp并得到奇怪的结果

我有一个列中的数字,从第25行开始。我想用这个列有数据的行号来填充一个数组。 下面的代码工作提供的数字是排列在很远的行。 为了简单起见,我假设在500行范围内不超过20个这样的行。 当行连续填充数据时,我得到奇怪的结果。 例如:在行45,46,47有数据,但是这个代码只返回行45和47.我是VBA的新手,很容易灰心。 请帮忙 :)

Dim li_lastrow, li_current_row, rowvalue As Integer Dim lb_finished As Boolean Dim a(1 To 20) As Integer i = 1 li_last_row = 500 lb_finished = False Do Until lb_finished Or (i = 20) rowvalue = Worksheets("Sheet1").Range("C" & li_last_row).End(xlUp).Row If rowvalue <= 25 Then lb_finished = True Else a(i) = rowvalue MsgBox a(i) Worksheets("Sheet1").Range("C" & li_last_row).Value = "" li_last_row = rowvalue i = i + 1 End If 

循环

向上到下一个单元格,它在空单元格和非空单元格之间变化。 它的行为就像Ctrl + Up。

 'Only the last is a integer the first two are variants 'Dim li_lastrow, li_current_row, rowvalue As Integer 'Is equal to: 'Dim li_lastrow 'Dim li_current_row 'Dim rowvalue As Integer Dim a(1 To 20) As Integer i = 1 Dim currentRow as Integer For currentRow = 500 to 26 Step -1 'If the row is empty go to the next nonempty row If Worksheets("Sheet1").Cells(currentRow ,"C").Value = "" then currentRow = Worksheets("Sheet1").Cells(currentRow ,"C").End(xlUp).Row end if a(i) = currentRow MsgBox a(i) Worksheets("Sheet1").Cells(currentRow ,"C").Value = "" 'Exit the for loop if we have 20 rows if i = 20 then exit for end if i = i + 1 Next