我怎样才能在Excel中逻辑testing另一个单元格的格式

我知道条件格式。 我想在相反的方向做testing。

为了简化,我想实质上做到这一点:

if ((Text_Align(A1)='left',"L","R") 

要么

 if ((Background_Color(A1)="Pink", "Red dominates the background", "Just another blah background") if ((Fontweight(A1)="Bold", "That was a strong statement", "A cell filled by a bean counter") 

到目前为止,我还没有find一个可以做Text_Align的函数 – 也就是testing一个单元格格式的值。

这可能吗?

你可以使用CELL函数获得关于格式等信息,例如:

 =CELL("format", H2) 

这支持许多其他的参数,而不是这里定义的格式。

但是,如果这个函数的选项没有返回你所需要的,那么你可能需要使用Bathsheba的答案中提出的VBA。

你可以在VBA中做到这一点。 这是一些原型代码:

 Public Function test(ByVal rng As Range) Application.Volatile Select Case rng.HorizontalAlignment Case xlLeft test = "L" Case xlRight test = "R" Case Else test = "?" End Select End Function 

注意行Application.Volatile 。 这将函数设置为volatile 。 它告诉VBA函数返回值不是单纯的input单元值的函数:格式化更改不会触发重新计算。 改变了一些格式,使用F9来重新计算工作簿。

除了使用VBAmacros,另一个select是使用内置的XL4macros,你可以在这里find更多的信息。 在这里你会发现一个来自XL4macros的函数,叫做Get.Cell函数来返回单元格的很多方面。

注意:你不需要下载上面的文件来实现这个工作,你可以按照我的下面的指示,但是那个文件包含了更多的function和信息。 当只有一个单元格的格式被改变时,这个方法仍然不会更新,所以在运行易失性macros的时候,这个方法不会更准确,尽pipe它可以节省你编写所有的macros的时间。 它将在每次重新计算工作表时更新,就像一个易失性子/macros/函数一样。

当你下载这个文件的时候,它会更详细地解释这个函数里面的所有东西。

但是问题是你不能从工作表中调用Get.Cell ,但是你可以从一个命名的范围中调用它。 所以你必须使用它如下

  1. 打开名称pipe理器
  2. input范围名称为Text_Align
  3. =GET.CELL(8,OFFSET(ACTIVE.CELL(),0,-1))此公式
  4. 点击OK

现在,您键入的任何单元格为=Text_Align ,它将返回单元格左边的单元格,您可以修改上述公式以引用所需的任何引用。

因此,如果在单元格B1键入=Text_Align ,则会返回有关A1的水平alignment的信息。 它将返回以下选项:

  1 = General 2 = Left 3 = Center 4 = Right 5 = Fill 6 = Justify 7 = Center across cells 

以下是上述链接的MacroFun帮助文件中的一些信息我只添加了与您在问题中提到的内容有关的信息:

 Returns information about the formatting, location, or contents of a cell. Use GET.CELL in a macro whose behavior is determined by the status of a particular cell. Syntax GET.CELL(type_num, reference) Type_num is a number that specifies what type of cell information you want. The following list shows the possible values of type_num and the corresponding results. Type_num Returns 8 Number indicating the cell's horizontal alignment: 1 = General 2 = Left 3 = Center 4 = Right 5 = Fill 6 = Justify 7 = Center across cells 13 Number from 0 to 18, indicating the pattern of the selected cell as displayed in the Patterns tab of the Format Cells dialog box, which appears when you choose the Cells command from the Format menu. If no pattern is selected, returns 0. 18 Name of font, as text. 19 Size of font, in points. 20 If all the characters in the cell, or only the first character, are bold, returns TRUE; otherwise, returns FALSE. 21 If all the characters in the cell, or only the first character, are italic, returns TRUE; otherwise, returns FALSE. 22 If all the characters in the cell, or only the first character, are underlined, returns TRUE; otherwise, returns FALSE. 23 If all the characters in the cell, or only the first character, are struck through, returns TRUE; otherwise, returns FALSE. 24 Font color of the first character in the cell, as a number in the range 1 to 56. If font color is automatic, returns 0. 25 If all the characters in the cell, or only the first character, are outlined, returns TRUE; otherwise, returns FALSE. Outline font format is not supported by Microsoft Excel for Windows. 26 If all the characters in the cell, or only the first character, are shadowed, returns TRUE; otherwise, returns FALSE. Shadow font format is not supported by Microsoft Excel for Windows. 38 Shade foreground color as a number in the range 1 to 56. If color is automatic, returns 0. 39 Shade background color as a number in the range 1 to 56. If color is automatic, returns 0. 40 Style of the cell, as text. 42 The horizontal distance, measured in points, from the left edge of the active window to the left edge of the cell. May be a negative number if the window is scrolled beyond the cell. 43 The vertical distance, measured in points, from the top edge of the active window to the top edge of the cell. May be a negative number if the window is scrolled beyond the cell. 44 The horizontal distance, measured in points, from the left edge of the active window to the right edge of the cell. May be a negative number if the window is scrolled beyond the cell. 45 The vertical distance, measured in points, from the top edge of the active window to the bottom edge of the cell. May be a negative number if the window is scrolled beyond the cell. 46 If the cell contains a text note, returns TRUE; otherwise, returns FALSE. 48 If the cells contains a formula, returns TRUE; if a constant, returns FALSE. 50 Number indicating the cell's vertical alignment: 1 = Top 2 = Center 3 = Bottom 4 = Justified 51 Number indicating the cell's vertical orientation: 0 = Horizontal 1 = Vertical 2 = Upward 3 = Downward 57 Returns TRUE if all the characters in the cell, or only the first character, are formatted with a superscript font; otherwise, returns FALSE. 58 Returns the font style as text of all the characters in the cell, or only the first character as displayed in the Font tab of the Format Cells dialog box: for example, "Bold Italic". 59 Returns the number for the underline style: 1 = none 2 = single 3 = double 4 = single accounting 5 = double accounting 60 Returns TRUE if all the characters in the cell, or only the first characrter, are formatted with a subscript font; otherwise, it returns FALSE. 63 Returns the fill (background) color of the cell. 64 Returns the pattern (foreground) color of the cell. 65 Returns TRUE if the Add Indent alignment option is on (Far East versions of Microsoft Excel only); otherwise, it returns FALSE. 

笔记

 Reference is a cell or a range of cells from which you want information. If reference is a range of cells, the cell in the upper-left corner of the first range in reference is used. If reference is omitted, the active cell is assumed. 

你可以这样做:= CELL(“prefix”,A1)

根据文档,前缀给你:

“前缀”与单元格的“标签前缀”相对应的文本值。 如果单元格包含左alignment文本,则返回单引号(');如果单元格包含右alignment文本,则返回双引号(“);如果单元格包含居中文本,则为加粗(^);如果单元格包含填充,则返回反斜杠alignment的文本,如果单元格包含其他内容,则为空文本(“”)。

所以你的答案是:

 =IF(CELL("prefix",A1)=CHAR(34),"R",IF(CELL("prefix",A1)="'","L","?"))