【问题标题】:next without for error excel VBA接下来没有错误excel VBA
【发布时间】:2015-02-02 14:08:00
【问题描述】:

我不明白为什么这段代码有编译错误 Next without For 请帮忙:(

Sub DefColorCodes()

    For i = 2 To 5
        Range("actReg").Value = Range("Sheet1!A" & i).Value
        ActiveSheet.Shapes.Range("actReg").Select
        With Selection.ShapeRange.Fill.ForeColor.RGB = Range(Range("actRegCode").Value).Interior.Color

    Next i

    Range("B17").Select
End Sub

【问题讨论】:

  • 你需要一个 End WithNext

标签: excel vba for-loop next


【解决方案1】:

你没有结束你的With。在 Next i 行之前的某个位置添加 End With 行。

Sub DefColorCodes()
    For i = 2 To 5
        Range("actReg").Value = Range("Sheet1!A" & i).Value
        ActiveSheet.Shapes.Range("actReg").Select
        With Selection.ShapeRange.Fill.ForeColor.RGB = Range(Range("actRegCode").Value).Interior.Color
        End With
    Next i
    Range("B17").Select
End Sub

在这种情况下,您很可能根本不想使用 With

Sub DefColorCodes()
    For i = 2 To 5
        Range("actReg").Value = Range("Sheet1!A" & i).Value
        ActiveSheet.Shapes.Range("actReg").Select
        Selection.ShapeRange.Fill.ForeColor.RGB = Range(Range("actRegCode").Value).Interior.Color
    Next i
    Range("B17").Select
End Sub

【讨论】:

  • @MiloKang:太好了!如果您可以接受我的回答,这将使用户知道该解决方案对您有用。欢迎来到 Stack Overflow!
【解决方案2】:

您在 for 循环中使用了 With 语句,而没有 End With

【讨论】:

    猜你喜欢
    • 2013-10-05
    • 1970-01-01
    • 2020-11-17
    • 2012-12-15
    • 1970-01-01
    • 2021-11-14
    • 2013-06-09
    • 2016-03-27
    相关资源
    最近更新 更多