【问题标题】:VBA: Invalid Next Control Variable ReferenceVBA:无效的下一个控制变量引用
【发布时间】:2014-03-08 17:18:56
【问题描述】:

基本上,我正在尝试使用为了我的目的而剥离并回收的代码来模拟连接结果。但是当脚本尝试处理“Next T”idk 时我遇到了问题,但我已经指示为 Dim - Integer,但这似乎仍然没有解决问题。

原始代码来源: Concatenate multiple ranges using vba

我在这件作品上遇到了很多问题,因为它似乎是我长期以来一直在尝试将其包含在我的脚本中的唯一内容。在关闭 If、调整 Then 甚至退出循环时出现编译错误。

我认为下一个应该是我最后的担忧。

顺便说一句,rnumbers 应该是一个值/整数的位置,但我也不完全确定这样做是否正确。

    rnumbers = Rows(ActiveCell.Range("A3").End(xlDown)) + 3
    'or CellCount = ActiveCell.Range("A" & Rows.Count).End(xldown).Row

    Do While Rows(ActiveCell.Range("A3").End(xlDown)) > 3

        'For Q = 1 To 10 'This provides a column reference to concatenate - Outer For statement
        For T = 3 To rnumbers 'This provides a rows reference to concatenate - Inner for statement

            For Each Cell In Cells("A" & T) 'provides rows and column reference
                If Cell.Value = "" Then
                    GoTo Line1   'this tells the macro to continue until a blank cell is reached
                    Exit For
                End If
                x = x & Cell.Value & Chr(10)   'This provides the concatenated cell value and comma separator
            'Next ' this loops the range

        Next T  'This is the inner loop which dynamically changes the number of rows to loop until a blank cell is reached

        Line1:
        On Error GoTo Terminate 'Terminates if there are less columns (max 10) to concatenate

        ActiveCell.Value = Mid(x, 1, Len(x) - 1) 'This basically removes the last comma from the last concatenated cell e.g. you might get for a range 2,3,4, << this formula removes the last comma to
    'give 2,3,4

        ActiveCell.Offset(1, 0).Select 'Once the concatenated result is pasted into the cell this moves down to the next cell, e.g. from F1 to F2

        x = ""  'The all important, clears x value after finishing concatenation for a range before moving on to another column and range


        'Next Q 'After one range is done the second column loop kicks in to tell the macro to move to the next column and begin concatenation range again

    'rnumbers = 0
    'Next
    Exit Do
    'Resume
    Terminate:'error handler

【问题讨论】:

    标签: vba excel compiler-errors concatenation


    【解决方案1】:

    再试一次...当我仔细查看您的代码时,我实际上使用了一个坏词。

    您一直与错误的人群混在一起,并且正在接受一些非常糟糕的代码结构想法。 GoTo 后跟 Exit For?后一种说法永远达不到!跳出For 循环是一件危险的事情(如果没有错的话)。是的,对于For Each 语句,您仍然需要一个Next(具有匹配的控制参数 - Next T 属于不同的 For 循环,而不是最里面的循环)。

    无论如何-我觉得the Cat In The Hat:“这个烂摊子如此之大,如此之深,如此之高-我们无法捡起它,根本没有办法!”。所以我决定给你盖一座新房子。

    我认为以下内容可以完成您想做的事情,而且非常优雅。看看它是否有意义,以及是否可以根据您的目的进行调整。我要睡觉了,但明天早上会看看你是否从这里弄明白了。

    Sub concAll()
    Dim allRows As Range, target as range
    Dim oneRow
    Dim nc as Integer
    
    Set allRows = Range("A3", "J10")  ' pick the real range here - dynamically, probably
    nc = allRows.Columns.Count        ' need this number later to know where to put result
    
    For Each oneRow In allRows.Rows   ' loop over one row of the range at a time
      Dim s As String
      s = ""                          ' start with empty string
      For Each c In oneRow.Cells      ' loop over all the cells in the row
    
        If Not IsEmpty(c) Then
          s = s & "," & c.Text
        Else
          Exit For                     ' done with this row: found empty cell
        End If
    
      Next c                           ' keep looping over the cells...
    
      Set target = oneRow.Cells(1).Offset(0, oneRow.Cells.Count) ' cell where we put result
      target.Value = Mid(s, 2)   ' put the concatenated value to the right of everything; 
                                 ' skipping first comma (which came before first text)
    Next oneRow                  ' repeat for all rows in source range
    
    
    End Sub
    

    【讨论】:

    • 感谢您抽出宝贵时间发表评论并帮助解决我的问题。您发表的第一条评论,关于下一条评论和活跃。原来这是一个额外的下一个,我注释掉了,为了帮助脚本在没有任何编译错误的情况下运行,我把那个注释留在那里提醒我所做的更改和调整。有助于提高我的进步和纠正技能,尤其是当我回顾时。我也将变量从 T 更改为“rcell”,这也提示了同样的错误。我发布了脚本的其余部分。还有其他想法吗?
    【解决方案2】:

    对不起,我应该解释我想要制作的东西,而不是要求修复我想做的事情。我在 vba 方面的经验是自学的,我对寻求帮助有点陌生。

    Floris 生成的脚本似乎具有功能,但并不符合预期。原来我写的有点过时了,需要擦除并重新启动。这实际上是我几个月前开始的一个旧脚本,它通过网络查询起作用。但是网站经过了一些更改,现在脚本到处都是。

    我遇到的主要问题是一个编译错误“无效的下一个控制变量引用”,结果证明这是由一个打开的“Do while”循环引起的,这似乎没有太多的研究退出点我抬头看。应该使用另一个“If”命令。同时,在尝试解决“Do While”时,我添加了一个额外的“Next”(因为我认为它们是兼容的),但脚本却搞砸了。

    很难解释.. 但是我使用的“Do While”,我希望它仅在值的数量更大的情况下合并值

    rnumbers = Rows(ActiveCell.Range("A3").End(xlDown)) + 3
    'or CellCount = ActiveCell.Range("A" & Rows.Count).End(xldown).Row
    
    Do While Rows(ActiveCell.Range("A3").End(xlDown)) > 3
    

    但它应该是

    Dim CellCount As Range
    
    CellCount = ActiveCell.Range("A" & Rows.Count).End(xlDown).Row + 2
    'cause its the active cell + two additional cells
    
    If CellCount > 3
    

    然后打开 Floris 提交的脚本。 (但这也失败了,因为上面所说的)。

    再次感谢,希望它能解释一切......对不起,如果我在那个 Floris 上浪费了您的时间,真的很感谢您的帮助。真希望我早点寻求帮助,这样可以省去我现在正在处理的很多挫败感。 >_>

    【讨论】:

      猜你喜欢
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      相关资源
      最近更新 更多