Getting Excel Cell Properties in Bulk

后端 未结 2 643
孤独总比滥情好
孤独总比滥情好 2020-12-06 12:54

When fetching values from a range in Excel, it is much more efficient to get the values in "bulk" (as a 2D array) than looping through each row and column. For ex

相关标签:
2条回答
  • 2020-12-06 13:20

    I don't think you can get those properties as an array because of how Excel stores that information. Excel does not store formatting for each cell individually but rather stores a particular blend of formats along with an internal "list" of the ranges that use that format.

    You can get a sense of how the formatting is stored by creating a small test file with various formats and saving it as XML format (in 2010 at least, you need to use "XML Spreadsheet 2003").

    This article may also help.

    0 讨论(0)
  • 2020-12-06 13:31

    "I want to find which cells are colored a certain way"

    in VBA you can run a quick routine using the Find method that searches by format. For example to find all cells with the same cell font colour and interior colour as the cell in A1. I presume you can use something similar in VSTO

    Sub FindFormat()
        Dim rng1 As Range
        Dim rng2 As Range
        Dim strAddress As String
        With Application.FindFormat
            .Interior.ColorIndex = [a1].Interior.ColorIndex
            .Font.Color = [a1].Font.Color
        End With
        Set rng1 = Cells.Find("", [a1], xlFormulas, , , , , , True)
        If Not rng1 Is Nothing Then
            strAddress = rng1.Address
            Set rng2 = rng1
            Do
                Set rng1 = Cells.Find("", rng1, xlFormulas, , , , , , True)
                Set rng2 = Union(rng1, rng2)
            Loop While rng1.Address <> strAddress
            MsgBox "Range similar format to A1 is " & rng2.Address
        End If
    End Sub
    

    enter image description here

    0 讨论(0)
提交回复
热议问题