【问题标题】:VBA error code : "For control variable already in use"VBA 错误代码:\“对于已在使用的控制变量\”
【发布时间】:2023-01-12 02:45:19
【问题描述】:

我在 Excel 中使用 VBA 我收到一条错误消息,指出控制变量已在使用中。当错误弹出第二个子组合()在 VBA 编辑器中突出显示。我试图获得不同列中的组合并消除列中具有空白单元格的行。希望我能解释一下。任何人都可以帮助更正此代码。谢谢

Sub combinations()
i = 1
For Each cella In Sheet3.Range("A:A").SpecialCells(xlCellTypeConstants)
    For Each cellb In Sheet3.Range("B:B").SpecialCells(xlCellTypeConstants)
        For Each cellc In Sheet3.Range("C:C").SpecialCells(xlCellTypeConstants)
            For Each cellc In Sheet3.Range("D:D").SpecialCells(xlCellTypeConstants)
                For Each cellc In Sheet3.Range("E:E").SpecialCells(xlCellTypeConstants)
                    For Each cellc In Sheet3.Range("F:F").SpecialCells(xlCellTypeConstants)
                        For Each cellc In Sheet3.Range("G:G").SpecialCells(xlCellTypeConstants)
                            For Each cellc In Sheet3.Range("H:H").SpecialCells(xlCellTypeConstants)
                                For Each cellc In Sheet3.Range("I:I").SpecialCells(xlCellTypeConstants)
                                    For Each cellc In Sheet3.Range("J:J").SpecialCells(xlCellTypeConstants)
                                        For Each cellc In Sheet3.Range("K:K").SpecialCells(xlCellTypeConstants)
                                            For Each cellc In Sheet3.Range("L:L").SpecialCells(xlCellTypeConstants)
                                                For Each cellc In Sheet3.Range("M:M").SpecialCells(xlCellTypeConstants)
                                                    Sheet9.Range("A" & i).Value = cella.Value
                                                    Sheet9.Range("B" & i).Value = cellb.Value
                                                    Sheet9.Range("C" & i).Value = cellc.Value
                                                    Sheet9.Range("D" & i).Value = cellc.Value
                                                    Sheet9.Range("E" & i).Value = cellc.Value
                                                    Sheet9.Range("F" & i).Value = cellc.Value
                                                    Sheet9.Range("G" & i).Value = cellc.Value
                                                    Sheet9.Range("H" & i).Value = cellc.Value
                                                    Sheet9.Range("I" & i).Value = cellc.Value
                                                    Sheet9.Range("J" & i).Value = cellc.Value
                                                    Sheet9.Range("K" & i).Value = cellc.Value
                                                    Sheet9.Range("L" & i).Value = cellc.Value
                                                    Sheet9.Range("M" & i).Value = cellc.Value
                                                    i = i + 1
                                                Next
                                            Next
                                        Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
        Next
    Next
Next

Set mysheet1 = Sheet9
    row1 = mysheet1.UsedRange.Rows.Count
    For i = row1 To 1 Step -1
    If Len(mysheet1.Cells(i, 1).Value) = 0 Then
      mysheet1.Cells(i, 1).EntireRow.Delete
    End If
Next

结束子

【问题讨论】:

  • 错误消息告诉您问题所在:您在许多嵌套的 for 循环中重复使用 cellc。你不能那样做
  • 1.另外,您可能遇到的下一个错误是SpecialCells(xlCellTypeConstants)。使用SpecialCells 时始终使用正确的错误处理2.您还可以解释一下您要达到的目标吗?我觉得可能有更好的方法来实现你想要的......
  • 请注意:如果每列只有 3 个值,这将为您提供 3^13 行,即 1.5m - Excel 无法处理。但是,如果不将 i 显式声明为 Long,则会更早出现运行时错误,因为没有声明 VBA 将隐式将其声明为整数,其限制为 32'768。也就是说:您可能会考虑递归算法而不是 13 个嵌套循环。
  • 欢迎 SP160477!请花一些时间阅读 Stack Overflow 的介绍并赢得您的下一个徽章。 stackoverflow.com/tour
  • Range(A:A) 的长度为 1048576 个单元格,因此循环执行的时间可能比您预期的要长。

标签: excel vba combinations multiple-columns


【解决方案1】:

您可以缩短循环以使用一行代码删除行:

On Error Resume Next
Sheet9.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

这将删除单元格 A 为空的每一行。需要抑制错误处理,因为如果 A 列中没有空单元格,则会发生错误。

我不确定你想用那个可怕的嵌套 for 循环做什么。但是其他人已经在 cmets 中告诉您这可能会失败。至少你必须将循环变量命名为唯一的,但我怀疑这是否能解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多