1

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

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

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

我得到的错误是 Next without for

4

1 回答 1

1

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 行和第 7 行也可以更改如下:

  Set R = ActiveWindow.Selection
  For Each P In R
于 2013-04-21T02:47:16.397 回答