Excel VBA Next 不带 For

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16110467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:44:16  来源:igfitidea点击:

Excel VBA Next Without For

excelvba

提问by varun Kishnani

The syntax of a new language can be difficult to mater. I am a 1 day old to VBA excel Wonder Where I am going wrong below.

一种新语言的语法可能很难掌握。我是一个 1 天大的 VBA excel 不知道我在下面哪里出错了。

Trying to color rows in an selection of Cells made by the user alternatively

尝试为用户选择的单元格中的行着色

Sub color()
    Dim R As Range
    Dim I As Integer
    Dim P As Object
    I = 1
    R = Selection
    For Each P In R
        If I Mod 2 = 0 Then
             P.Interior.ColorIndex = 36
        Else
            P.Interior.ColorIndex = 40
        I = I + 1
    Next P
End Sub

The error I get is Next Without for

我得到的错误是 Next without for

回答by Pruthviraj

Siddarth, his question was "The error I get is Next Without for". I had corrected the error by adding "End IF". Anyways here is the correct code: ---

Siddarth,他的问题是“我得到的错误是 Next without for”。我通过添加“End IF”更正了错误。无论如何这里是正确的代码:---

Sub color()
  Dim R As Range
  Dim I As Integer
    Dim P As Object
  I = 1
  'R = ActiveWindow.Selection
  For Each P In ActiveWindow.Selection
      If I Mod 2 = 0 Then
           P.Interior.ColorIndex = 36
      Else
          P.Interior.ColorIndex = 40
      End If
      I = I + 1
    Next P
 End Sub

6 and 7th line also can be changed as below:

第 6 行和第 7 行也可以更改如下:

  Set R = ActiveWindow.Selection
  For Each P In R