为什么"范围(...)。结束(xlDown).Row"返回1048576?

时间:2015-08-24 21:53:45

标签: excel vba

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

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

可悲的是Range(...).End(xlDown).Row返回1048576。当我调试时,突然值变为正确值。相同的代码在VBA代码中的其他一些位置也能很好地工作。

2 个答案:

答案 0 :(得分:5)

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

从A1开始:

enter image description here

点击Ctrl+Down

enter image description here

这说明当您在由多个单元组成的块的开头或结尾使用Ctrl+Down时会发生什么 - 您将转到该块的末尾。当您说有时代码运行良好时,您就是隐含地提到的情况。

现在 - 再次点击Ctrl+Down

enter image description here

你跳到了下一个街区。现在,再来一次:

enter image description here

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

enter image description here

嗯 - 没有下一个要去的地方 - 所以它一直向下。这对应于让你觉得奇怪的情况。它把你带到这里:

enter image description here

但是 - 现在很酷的事情发生了:按Ctrl + Up - Excel将向上搜索下一个区块:

enter image description here

是A列中包含数据的最后一个单元格。

出于这个原因 - 您在Excel VBA中看到了以下代码:

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

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

答案 1 :(得分:0)

您对在调试时获得正确值的说法很好奇。这让我觉得您可能需要显示更多代码。无论如何,没关系,我只是决定对此有一些乐趣。以下是多个示例代码段,可以执行您要求的操作。希望它能让你或其他任何人对如何处理这个问题感兴趣,因为它很常见。

我绝对不会单独使用End(xlwhatever)。它会产生非健壮的代码,因为它很容易被洗牌。花时间创建更优雅的解决方案并不难。您应该能够通过定制和组合以下方法来为您的特定用途提供非常强大的功能。

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
相关问题