English 中文(简体)
excel - vba - 2. 有条件组合的囚室
原标题:excel - vba - Get cell color of conditionally formatted cell

I m 试图利用VBA 书写器取得细胞色

Public Function GetCellColor(cell As Range) As Long
    Application.Volatile
    GetCellColor = cell.Interior.Color
End Function

这套工作是人工包装的电池。 但是,如果我使用一个有条件的单元,那么它只是显示单元的价值,没有格式。

因此,当我试图使用下文中本应找到有条件组合的囚室的VBA。 但是,它给我一个“#VALUE”错误。 我在下面的文字上做了什么错误?

Function GetCellColor(cell As Range) As String
    Application.Volatile
    GetCellColor = cell.DisplayFormat.Interior.Color
End Function
问题回答

下面是UDF,它将给你电池填写彩色(包括从有条件格式上填充的颜色),而没有这个编号#VALUE差错<<>>>。

Option Explicit

Public Function CellFillColor(target As Range, Optional returnFormat As String = "IDX") As Variant
Dim retArray()
Dim rowCounter As Long
Dim colCounter As Long
Dim colorValue As Long
     Application.Volatile
    If TypeName(target) = "Range" Then
        ReDim retArray(target.Rows.Count - 1, target.Columns.Count - 1)
        For rowCounter = 0 To target.Rows.Count - 1
            For colCounter = 0 To target.Columns.Count - 1
                colorValue = Evaluate("useDF(" & target.Cells(rowCounter + 1, colCounter + 1).Address & ")")
                Select Case UCase(returnFormat)
                    Case "RGB":
                                retArray(rowCounter, colCounter) = _
                                                                    Format((colorValue Mod 256), "00") & ", " & _
                                                                    Format(((colorValue  256) Mod 256), "00") & ", " & _
                                                                    Format((colorValue  65536), "00")
                    Case "HEX":
                                retArray(rowCounter, colCounter) = _
                                                                    "#" & _
                                                                    Format(Hex(colorValue Mod 256), "00") & _
                                                                    Format(Hex((colorValue  256) Mod 256), "00") & _
                                                                    Format(Hex((colorValue  65536)), "00")
                    Case "IDX": retArray(rowCounter, colCounter) = colorValue
                    Case Else: retArray(rowCounter, colCounter) = colorValue
                End Select
            Next colCounter
        Next rowCounter
        CellFillColor = retArray  IIf(target.CountLarge = 1, retArray(0, 0), retArray)
    End If
End Function

Private Function useDF(ByVal target As Range) As Variant
    useDF = target.DisplayFormat.Interior.Color
End Function

 in Immediate Window
 Range("G16").Interior.Color=13551615<-IDX value

另见我的

它基于Jaafar Tribak。 s 代码,他在

I hope this helps.
Feel free to take it apart and/or rearrange it as above code was written for giving more choices to the user thereby making it (unnecessarily?) longer.





相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...