为什么“Range(…).End(xlDown).Row”返回1048576?

我想在VBA中的for循环中复制一些值。 因此,我通过以下方式计算极限

For iCounter = 1 To (Range(...).End(xlDown).Row - Range(...).Row + 1) 

可悲的Range(...).End(xlDown).Row返回1048576 。 当我debugging时,值突然改变到正确的一个。 在VBA代码中,相同的代码在其他一些地方也能很好地工作。

要记住的主要是End方法在VBA中重现使用Ctrl +箭头键的function。 这些是为了在值块之间导航。

从A1开始:

在这里输入图像描述

Ctrl+Down

在这里输入图像描述

这说明了当你在由多个单元组成的块的开始或结束处使用Ctrl+Down时会发生什么 – 你到那个块的结尾。 当你说有时代码运行良好的时候,你就隐含地指的是这种情况。

现在再次Ctrl+Down

在这里输入图像说明

你跳到下一个块。 现在,再做一次:

在这里输入图像描述

该块的底部。 最后再一次:

在这里输入图像说明

那么 – 没有下一个块 – 所以它一直下降。 这相当于让你感到奇怪的情况。 它把你放在这里:

在这里输入图像描述

但是 – 现在发生了一件很酷的事情:按Ctrl + Up – 然后Excelsearch下一个块:

在这里输入图像说明

是A列数据的最后一个单元格

由于这个原因 – 你在Excel VBA中看到很多下面的代码:

 Cells(Rows.Count,1).End(xlUp) 

要获取列中最后使用的单元格(在本例中为1)或列中第一个单元格(如果整个列为空白)。

对于在debugging时获得正确的值,你很想知道你在说什么。 这让我觉得你可能需要展示更多的代码。 无论如何,没关系,我只是决定玩一下这个。 这里有多个示例代码片段来做你所问的。 希望它给你或任何其他人感兴趣的如何解决这个问题的一些想法,因为它是非常普遍的。

我一定会避免使用End(xlwhatever)。 它使非稳健的代码,因为它很容易被打乱。 花时间创build更优雅的解决scheme并不难。 你应该能够想出一些相当强大的东西,通过调整和组合下面的方法来满足你的特定用途。

 Sub CellLoop1() 'Colors cells from row i to last value in column For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row Cells(i, 1).Interior.Color = RGB(0, 200, 0) Next End Sub Sub CellLoop2() 'Colors cells from row i to end of continuous values or until row 100 Dim i As Integer i = 1 Dim r As Range Set r = Cells(i, 1) Do Until r.Value2 = "" Or i > 100 r.Interior.Color = 123 i = i + 1 Set r = Cells(i, 1) Loop End Sub Sub CellLoop3() 'Colors cells from row i until occurance of 5 continuous blanks Dim i As Integer i = 1 Dim r As Range Set r = Cells(i, 1) Dim BlankChain As Integer Do Until BlankChain >= 5 r.Interior.Color = 123 If r.Value = Empty Then BlankChain = BlankChain + 1 Else BlankChain = 0 End If i = i + 1 Set r = Cells(i, 1) Loop End Sub Sub CellLoop4() 'Colors cells from row i until no value in sight (in next k number of rows) Dim i, k, BlankCount As Integer i = 1 k = 10 Dim r, SightRange As Range Set r = Cells(i, 1) Dim NoValueInSight As Boolean: NoValueInSight = False Do Until NoValueInSight Set SightRange = Range(r, r.Offset(k - 1, 0)) BlankCount = Application.WorksheetFunction.CountBlank(SightRange) If BlankCount = SightRange.Rows.Count Then NoValueInSight = True Else r.Interior.Color = RGB(255, 50, 255) End If i = i + 1 Set r = Cells(i, 1) Loop End Sub Sub CellLoop5() 'Colors all values under range r (set as "A1") Dim r, UnderRange As Range Set r = Range("A3") Set UnderRange = Range(r, Cells(Rows.Count, 1)) Dim i, n, BlankCount As Double: i = r.Row: n = 0 BlankCount = Application.WorksheetFunction.CountBlank(UnderRange) Do Until n = (UnderRange.Rows.Count - BlankCount) If Cells(i, 1) <> "" Then n = n + 1 Cells(i, 1).Interior.Color = RGB(200, 200, 200) End If i = i + 1 Loop End Sub Sub CellLoop6() 'Colors all values under range r (set as "A1") Dim r As Range Set r = Range("A1") If r.Value = "" Then Set r = r.End(xlDown) Do Until r.Value = "" r.Interior.Color = RGB(255, 100, 100) If r.Offset(1, 0).Value <> "" Then For i = r.Row To r.End(xlDown).Row Cells(i, 1).Interior.Color = RGB(255, 100, 100) Next End If Set r = r.End(xlDown) Loop End Sub