Excel:具有特定单元格选择的 'for loops' 的语法是什么?

Excel: What is the syntax for 'for loops' with a specific selection of cells?

我有一个 Excel 项目有点问题。

我将 select 一组单元格,我想要一个宏,当运行时有一个 for 循环检查所有包含的单元格以查看它们的样式是否为 'good' 以及它是否即,总箱加一

在此示例中,当我 select 此 table 中的单元格并按下按钮时,总计下方的单元格应显示为 5,因为 table 中的 5 个单元格具有'good' 主题。

(比方说,'Total' 单元格下的单元格是 D5)

谢谢!

不清楚 'good style' 是什么,但您可以尝试类似的方法:

Sub CheckStyle()

  Dim rng As Range
  Dim total As Integer

  If TypeName(Selection) = "Range" Then
    For Each rng In Selection
      'enter any 'good' style defintion below
      If rng.Style = "Good" Then
        total = total + 1
      End If
    Next rng

    'change d5 to result cell address below
    ActiveSheet.Range("d5").Value = total

  End If
End Sub