1) How do I get the color of a cell using an Excel Macro? I couldn\'t get this function work:
Sub BGCol(MRow As Integer, MCol As Integer)
bgColor = Cells(M
Alternatively, if you do not want to use Macros or VBA, you can get the Cell Color using Aspose.Cells API.
Cell Color or Cell Fill Color is represented by
Cell Font Color is represented by
Sample Excel file used inside the Code
Please consider the following sample Excel file shown inside the snapshot. Here the cell C4 is filled with Yellow color and its font color is Red.
The following code in C# and Java loads the sample Excel file as shown above and accesses the cell C4 and its Style object. Then it prints the cell fill color i.e. Yellow and cell font color i.e. Red.
Please also see the Console Output given below and read the Comments inside the code for more understanding.
C#
// Directory path for input Excel file.
string dirPath = "D:/Download/";
// Load the input Excel file inside workbook object.
Aspose.Cells.Workbook wb = new Workbook(dirPath + "SampleExcelColor.xlsx");
// Access first worksheet.
Worksheet ws = wb.Worksheets[0];
// Access cell C4 by name.
Cell cell = ws.Cells["C4"];
// Access cell style.
Style st = cell.GetStyle();
// Print fill color of the cell i.e. Yellow.
// Please note, Yellow is (R=255, G=255, B=0)
Console.WriteLine(st.ForegroundColor);
// Print font color of the cell i.e. Red.
// Please note, Red is (R=255, G=0, B=0)
Console.WriteLine(st.Font.Color);
Console Output - C#
Color [A=255, R=255, G=255, B=0]
Color [A=255, R=255, G=0, B=0]
Java
// Directory path for input Excel file.
String dirPath = "D:/Download/";
// Load the input Excel file inside workbook object.
com.aspose.cells.Workbook wb = new Workbook(dirPath + "SampleExcelColor.xlsx");
// Access first worksheet.
Worksheet ws = wb.getWorksheets().get(0);
// Access cell C4 by name.
Cell cell = ws.getCells().get("C4");
// Access cell style.
Style st = cell.getStyle();
// Print fill color of the cell i.e. Yellow.
// Please note, Yellow is (R=255, G=255, B=0)
System.out.println(st.getForegroundColor());
// Print font color of the cell i.e. Red.
// Please note, Red is (R=255, G=0, B=0)
System.out.println(st.getFont().getColor());
Console Output - Java
com.aspose.cells.Color@ffffff00
com.aspose.cells.Color@ffff0000
You should use a Function:
Function BGCol(MRow As Integer, MCol As Integer) As Integer
BGCol = Cells(MRow, MCol).Interior.ColorIndex
End Function
Function GetColor(Mycell As Range)
GetColor = Mycell.Interior.ColorIndex
End Function
:::Use Formula:: =getcolor(x4)