Description
The CELL function returns information about the formatting, location, or contents of a cell.
Syntax
CELL(info_type, [reference])
Parameters
info_type Required. A text value that specifies what type of cell information you want to return. The following list shows the possible values of the info_type argument and the corresponding results.
info_type | Returns |
---|---|
"address" | Reference of the first cell in absolute reference, as text. eg. $B$1. |
"col" | Column number of the cell in reference. eg. 3. |
"color" | The value 1 if the cell is formatted in color for negative values; otherwise returns 0 (zero). |
"contents" | Value of the upper-left cell in reference; not a formula. |
"filename" | Filename (including full path) of the file that contains reference, as text. Returns empty text ("") if the worksheet that contains reference has not yet been saved. |
"format" | Text value corresponding to the number format of the cell. The text values for the various formats are shown in the following table. Returns "-" at the end of the text value if the cell is formatted in color for negative values. Returns "()" at the end of the text value if the cell is formatted with parentheses for positive or all values. |
"parentheses" | The value 1 if the cell is formatted with parentheses for positive or all values; otherwise returns 0. |
"prefix" | Text value corresponding to the "label prefix" of the cell. Returns single quotation mark (') if the cell contains left-aligned text, double quotation mark (") if the cell contains right-aligned text, caret (^) if the cell contains centered text, backslash () if the cell contains fill-aligned text, and empty text ("") if the cell contains anything else. |
"protect" | The value 0 if the cell is not locked; otherwise returns 1 if the cell is locked. |
"row" | Row number of the cell in reference. |
"type" | Text value corresponding to the type of data in the cell. Returns "b" for blank if the cell is empty, "l" for label if the cell contains a text constant, and "v" for value if the cell contains anything else. |
"width" | Column width of the cell, rounded off to an integer. Each unit of column width is equal to the width of one character in the default font size. |
The following list describes the text values that the CELL function returns when the info_type argument is "format" and the reference argument is a cell that is formatted with a built-in number format.
If the Excel format is | The CELL function returns |
---|---|
General | "G" |
0 | "F0" |
#,##0 | ",0" |
0.00 | "F2" |
#,##0.00 | ",2" |
$#,##0_);($#,##0) | "C0" |
$#,##0_);[Red]($#,##0) | "C0-" |
$#,##0.00_);($#,##0.00) | "C2" |
$#,##0.00_);[Red]($#,##0.00) | "C2-" |
0% | "P0" |
0.00% | "P2" |
0.00E+00 | "S2" |
# ?/? or # ??/?? | "G" |
m/d/yy or m/d/yy h:mm or mm/dd/yy | "D4" |
d-mmm-yy or dd-mmm-yy | "D1" |
d-mmm or dd-mmm | "D2" |
mmm-yy | "D3" |
mm/dd | "D5" |
h:mm AM/PM | "D7" |
h:mm:ss AM/PM | "D6" |
h:mm | "D9" |
h:mm:ss | "D8" |
reference Optional. The cell that you want information about. If omitted, the information specified in the info_type argument is returned for the last cell that was changed. If the reference argument is a range of cells, the CELL function returns the information for only the upper left cell of the range.
Remarks
If the info_type argument in the CELL function is "format" and you later apply a different format to the referenced cell, you must recalculate the worksheet to update the results of the CELL function.
Examples
General example
The example may be easier to understand if you copy the example data (include header) in the following table, and paste it in cell A1 of a new Excel worksheet. If you need to, you can adjust the column widths to see all the data.
Data | Formula | Result | Description |
---|---|---|---|
8-Jun-22 | =CELL("format", A2) |
D1 | The format code of cell A2 (d-mmm-yy) |
ExcelBaby.com | =CELL("contents", A3) |
ExcelBaby.com | The content of cell A3 |
=CELL("type",A4) |
b | The data type of cell A4 (blank) | |
Excel 2019 | =CELL("row",A5) |
5 | The row number of cell A5 |
Excel Courses | =CELL("col", A6) |
1 | The column number of cell A6 |
Get active workbook name
The "filename" argument returns full path of the file that contains reference, the workbook name enclosed in square brackets []
, so, we can use FIND function and MID function to get the workbook name.
=MID(LEFT(CELL("filename"),FIND("]",CELL("filename"))-1),FIND("[",CELL("filename"))+1,LEN(CELL("filename")))
For example:
=CELL("filename")
returns "C:\My Documents\[CELL function.xlsx]Examples
", this formula returns the workbook name: CELL function.xlsx
.
Get active worksheet name
The "filename" argument returns full path of the file that contains reference (including worksheet name), the workbook name enclosed in square brackets []
, so, we can use FIND function and MID function or REPLACE function to get the worksheet name.
=REPLACE(CELL("filename"),1,FIND("]",CELL("filename")),)
or
=MID(CELL("filename"),FIND("]",CELL("filename"))+1,255)
For example:
=CELL("filename")
returns "C:\My Documents\[CELL function.xlsx]Examples
", this formula returns the worksheet name: Examples
.
Check if active cell is in active worksheet
If the active cell is in another worksheet, the Address argument returns the worksheet name, so, we can use this feature to check if active cell is in active worksheet.
=LEFT(CELL("Address"),1)="$"
If the formula returns TRUE
, the active cell is in active worksheet, otherwise the active cell is in another worksheet.