簡體   English   中英

在Excel for Mac中讀取有多少單元格具有特定顏色

[英]Read how many cells have a certain color in Excel for Mac

我正在使用Office 2011 for Mac,在Excel應用程序中,是否有可能以某種方式讀取連續多少個單元格是紅色的?

這是一個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}會運行得更快,但我無法弄明白確切的措辭。

使用帶有以下代碼的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM