You can do this, but itโs not trivial, so Iโm not sure if itโs worth the fuss trying to simulate this behavior.
Basically, you need to create a macro in Excel that does the job, and then just return the results after the macro completes. This basically emulates the behavior of Value . I'm not quite sure why MS decided not to implement all the properties in Range to behave the same way, wierd.
To do this, you need to refer to the Microsoft Visual Basic for Application Extensibility 5.3 COM library. This gives you the tools you need to dynamically build and add macros to an Excel workbook.
The first step is to create a method that adds the module and the desired macro to the workbook you are interacting with (you can also open an empty workbook and create a macro there if you use this function on different sheets that you use opening and closing).
The following example adds a macro that returns an array with all the colors of the cells in the specified range. It is written in VBA, I do not have VS with me, but the C # version should be quite simple for the port.
Sub AddCode() Dim wb As Workbook Dim xPro As VBIDE.VBProject Dim xCom As VBIDE.VBComponent Dim xMod As VBIDE.CodeModule Set wb = ActiveWorkbook With wb Set xPro = .VBProject Set xCom = xPro.VBComponents.Add(vbext_ct_StdModule) Set xMod = xCom.CodeModule With xMod .AddFromString "Public Function GetInteriorColors(r As Range) As Variant" & vbCrLf & _ " Dim colors() As Long" & vbCrLf & _ " Dim row As Integer" & vbCrLf & _ " Dim column As Integer" & vbCrLf & _ " ReDim colors(r.Rows.Count - 1, r.Columns.Count - 1)" & vbCrLf & _ " For row = 1 To r.Rows.Count" & vbCrLf & _ " For column = 1 To r.Columns.Count" & vbCrLf & _ " colors(row - 1, column - 1) = r.Cells(row, column).Interior.Color" & vbCrLf & _ " Next" & vbCrLf & _ " Next" & vbCrLf & _ " GetInteriorColors = colors" & vbCrLf & _ "End Function" End With End With End Sub
Something noticeable; Sometimes I get a weird error when executing AddFromString . The macro code is added correctly to the module, but sometimes I get an error message; swallowing it does not seem harmful, but if you have the same problem, I would look at it.
Now that you have the macro, returning the result is easy (again, written in VBA):
Public Function GetColors(r As Range) As Long() //Note the absolute path to the macro; this is probably needed if the macro is in a different workbook. GetColors = Application.Run(ActiveWorkbook.Name & "!GetInteriorColors", r) End Function