简体   繁体   English

Excel VBA For Loop Error

[英]Excel VBA For Loop Error

I'm trying to run a simple For loop which will be expanded to include more functionality later but having trouble as it keeps throwing an error "invalid next control variable reference". 我正在尝试运行一个简单的For循环,它将被扩展为包含更多功能,但是因为它不断抛出错误“无效的下一个控制变量引用”而遇到麻烦。 The code I am trying to use is listed below. 我试图使用的代码如下所示。

Sub Tickbox()

Set Location = Sheets("TickBoxSheet").Range("B:B")

i = WorksheetFunction.CountA(Location)

Sheets("TickBoxSheet").Range("B2").Select


For a = 1 To i
    If Selection.Value = "True" Then
        Row = Selection.Row
        'Hide some rows in another sheet via if statements

        ActiveCell.Offset(1, 0).Select
    End If
Next i

End Sub

I don't know if I need more coffee this morning but I can't seem to figure out what the hell is going on. 我不知道今天早上是否需要更多咖啡,但我似乎无法弄清楚到底是怎么回事。 Any help will be greatly appreciated. 任何帮助将不胜感激。

The incremented variable (in Next ) should be the index variable, ie: 递增的变量(在Next )应该是索引变量,即:

For a = 1 To i
  '...
Next a

i is so popular as index that you should think twice before using it in other contexts. i是如此受欢迎的索引,你应该在其他情况下使用它之前三思而后行。

You have already got your answer from llmo. 你已经从llmo得到了答案。 However there are few other things I would like to stress upon... 然而,我想强调的其他一些事情......

Try and avoid .Select . 尽量避免.Select It will slow down your code. 它会降低你的代码速度。

Also It is not necessary that WorksheetFunction.CountA(Location) will give you the last row considering that you want to loop through all the rows which have data. 另外,考虑到要遍历所有具有数据的行, WorksheetFunction.CountA(Location) 不必为您提供最后一行。 I suggest this 我建议这个

Sub Tickbox()
    Dim i As Long, a As Long, Rw As Long

    With Sheets("TickBoxSheet")
        i = .Range("B" & .Rows.Count).End(xlUp).row

        For a = 2 To i
            If .Range("B" & a).Value = "True" Then
                Rw = a
                'Hide some rows in another sheet via if statements
            End If
        Next a
    End With
End Sub

You can make it more fast using Autofilter as well so that you loop through cells which only have True For example 你可以使用Autofilter更快地使它更快,这样你就可以遍历只有True单元格

Sub Tickbox()
    Dim i As Long, a As Long, Rw As Long
    Dim Location As Range, acell As Range

    With Sheets("TickBoxSheet")
        '~~> Remove any filters
        .AutoFilterMode = False

        i = .Range("B" & .Rows.Count).End(xlUp).row

        With .Range("B1:B" & i)
            .AutoFilter Field:=1, Criteria1:="True"
            Set Location = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
            Debug.Print Location.Address
        End With

        '~~> Remove any filters
        .AutoFilterMode = False

        For Each acell In Location
            If acell.Value = "TRUE" Then
                Rw = acell.row
                'Hide some rows in another sheet via if statements
            End If
        Next acell
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM