【问题标题】:Excel 2010 VBA Code not runningExcel 2010 VBA 代码未运行
【发布时间】:2017-05-23 03:55:41
【问题描述】:

我目前正在使用 Excel 2010 并尝试运行一些我在 VBA for Applications 中组合的代码(在按下 alt+F11 后)。我在记事本中输入了代码,双击我想要处理的项目后出现的记事本。我还将所有内容保存为 Excel 启用宏的工作簿 (*.xlsm)。

如果列 S、T 和 U 符合条件,我正在尝试将 D 列的背景着色为绿色或红色。如果列的值都为 0,则单元格 D 应为绿色。如果不是,它应该是红色的。

Sub GreenOrRed()
  Dim i As Integer
  For i = 2 To i = 27293
    If (Cells(i, "S").Value = 0 And Cells(i, "T").Value = 0 And Cells(i, "U").Value = 0) Then
        Cells(i, "D").Interior.ColorIndex = 10
    Else
        Cells(i, "D").Interior.ColorIndex = 9
    End If
   Next i
End Sub

代码运行并且没有抛出任何错误,但它也没有做任何事情。我做错了什么?

【问题讨论】:

  • 只是为了让您了解为什么 For 循环是有效的,但没有做任何事情......您的声明实际上是 For i = 2 To (i = 27293)。在循环初始化时,i 为 0,因此 i = 27293 等于 FalseFalse,当被强制转换为 Integer 时,为 0。因此,您的语句将变为 For i = 2 To 0。因为 2 已经大于 0,所以控制立即跳转到 Next i 之后的语句。
  • 大家好,感谢您的回复。我发现我的问题是我没有将“Worksheets(Sheet).Cells”放在所有单元格之前。一旦我这样做了,我就能让我的东西正常工作。我还将函数从 For 循环更改为 While 循环。

标签: vba excel excel-2010


【解决方案1】:

您在 For 循环中错误地使用了计数器。应该是这样的……

For i = 2 To 27293

【讨论】:

    【解决方案2】:

    已更改 For condition。 试试这个:-

    Sub GreenOrRed()
      Dim i As Integer
      For i = 2 To 27293
        If (Cells(i, "S").Value = 0 And Cells(i, "T").Value = 0 And Cells(i, "U").Value = 0) Then
            Cells(i, "D").Interior.ColorIndex = 10
        Else
            Cells(i, "D").Interior.ColorIndex = 9
        End If
       Next i
    End Sub
    

    【讨论】:

      【解决方案3】:

      稍微不同的方法:

      Sub GreenOrRed()
          Dim r As Range, rr As Range
          Set rr = Range("D1:D27293")
      
          For Each r In rr
              If r.Offset(0, 15).Value = 0 And r.Offset(0, 16).Value = 0 And r.Offset(0, 17).Value = 0 Then
                  r.Interior.ColorIndex = 10
              Else
                  r.Interior.ColorIndex = 9
              End If
          Next r
      End Sub
      

      【讨论】:

        【解决方案4】:

        您可以考虑设置一个(或两个)条件格式规则。

        Option Explicit
        
        Sub GreenOrRed()
            With ActiveSheet
                With .Range(.Cells(2, "D"), .Cells(.Rows.Count, "D").End(xlUp))
                    .Interior.ColorIndex = 9
                    .FormatConditions.Delete
                    With .FormatConditions.Add(Type:=xlExpression, Formula1:="=and(sum($S2)=0, sum($T2)=0, sum($U2)=0)")
                        .Interior.ColorIndex = 10
                        .StopIfTrue = True
                    End With
                End With
            End With
        End Sub
        

        我使用了单独的 SUM 函数来确保任何文本都返回零数值。

        备用自动筛选方法。

        Sub GreenOrRedFiltered()
            With ActiveSheet
                If .AutoFilterMode Then .AutoFilterMode = False
                With .Range(.Cells(1, "D"), .Cells(.Rows.Count, "D").End(xlUp)).Resize(, 18)
                    .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).Columns(1).Interior.ColorIndex = 9
                    .AutoFilter Field:=16, Criteria1:=0, Operator:=xlOr, Criteria2:=vbNullString
                    .AutoFilter Field:=17, Criteria1:=0, Operator:=xlOr, Criteria2:=vbNullString
                    .AutoFilter Field:=18, Criteria1:=0, Operator:=xlOr, Criteria2:=vbNullString
                    With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                        If CBool(Application.Subtotal(103, .Cells)) Then
                            .Columns(1).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 10
                        End If
                    End With
                End With
                If .AutoFilterMode Then .AutoFilterMode = False
            End With
        End Sub
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-31
          • 1970-01-01
          • 2018-06-24
          相关资源
          最近更新 更多