【问题标题】:Read how many cells have a certain color in Excel for Mac在 Excel for Mac 中读取有多少单元格具有某种颜色
【发布时间】:2013-02-04 19:10:35
【问题描述】:

我在 Excel 应用程序中使用 Office 2011 for Mac,是否有可能以某种方式读取一行中有多少个单元格是红色的?

【问题讨论】:

    标签: macos excel


    【解决方案1】:

    这是一个 AppleScript,它返回具有红色填充的单元格的列号和行号。还是您在寻找 AppleScript 以外的语言?

    tell application "Microsoft Excel"
        tell active sheet
            set lastColumn to used range's last column's first column index
            set lastRow to used range's last row's first row index
    
            set redCells to {}
            repeat with columnCounter from 1 to lastColumn
                repeat with rowCounter from 1 to lastRow
                    if column columnCounter's row rowCounter's interior object's color is {255, 0, 0} then
                        copy {columnCounter, rowCounter} to end of redCells
                    end if
                end repeat
            end repeat
    
            return redCells
        end tell
    end tell
    

    我敢打赌,像 tell application "Microsoft Excel" to tell active sheet to return every cell of used range whose interior object's color is {255, 0, 0} 这样的单行脚本会运行得更快,但我不知道确切的措辞。

    【讨论】:

    • 我正在寻找适用于 mac os x 的任何东西;我不关心语言,除非它有效或者我需要安装任何额外的语言/东西,
    • 由于您使用的是 Mac,因此无需安装任何额外内容。只需将代码粘贴到 AppleScript 编辑器中即可。
    【解决方案2】:

    这可以通过以下代码使用 VBA。将它放在一个模块中,然后从您要检查的工作表中运行它。

    Sub GetRedCells()
        Dim rCell As Range, rRowRange As Range, iRow As Integer, iInteriorColour As Integer, iFontColour As Integer
    
        iRow = Application.InputBox("Enter Row Number")
    
        If iRow > 0 Then
            Set rRowRange = ActiveSheet.Range(ActiveSheet.Cells(iRow, "A"), ActiveSheet.Cells(iRow, ActiveSheet.UsedRange.Columns.Count))
    
            For Each rCell In rRowRange
                If rCell.Interior.Color = vbRed Then iInteriorColour = iInteriorColour + 1
                If rCell.Font.Color = vbRed Then iFontColour = iFontColour + 1
            Next rCell
    
            MsgBox "You have " & iInteriorColour & " cell(s) that have a red interior " & vbCr & "and " & iFontColour & " cell(s) with a red font."
        End If
    End Sub
    

    【讨论】:

    • 是的,Office 2011 for Mac 有 VBA,只需转到工具 -> 宏 -> Visual Basic 编辑器。然后在 VBE 左侧的项目窗口中右键单击项目树中的任意位置并插入 -> 模块。粘贴代码,您可以从工具 -> 宏 -> 宏菜单中运行它或指定快捷键
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2020-01-19
    • 2012-07-28
    • 2019-02-10
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多