如何在Excel中逻辑测试另一个单元格的格式

时间:2014-02-04 13:43:46

标签: macos excel

我知道条件格式。我想以相反的方向进行测试。

为了简化,我想基本上这样做:

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")

到目前为止,我还没有找到能够完成Text_Align相同功能的函数 - 也就是说,测试单元格格式的值。

这可能吗?

4 个答案:

答案 0 :(得分:2)

您可以在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函数返回值不仅仅是输入单元格值的函数:格式化更改不会触发重新计算。更改了一些格式后,使用F9重新计算工作簿。

答案 1 :(得分:1)

您可以使用CELL函数获取有关像格式等单元格的一些信息,例如:

=CELL("format", H2)

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

但是,如果此功能的选项没有返回您需要的内容,那么您可能需要使用Bathsheba答案中提出的VBA。

答案 2 :(得分:0)

除了使用VBA宏之外,另一个选择是使用内置的XL4宏,您可以找到更多的infroamtion Here。您将在XL4宏中找到一个名为Get.Cell函数的函数,以返回单元格的许多方面。

注意: 不要需要下载上述文件才能正常工作您可以按照我的以下说明操作,但该文件包含更多功能和信息。此外,当只更改单元格的格式时,此方法仍然不会更新,因此这不会比运行Volatile宏更准确,尽管它确实可以节省编写所有宏的需要。每次工作表重新计算时 WILL 都会更新,就像挥发子/宏/函数一样。

当您下载文件时,它将更详细地解释函数内部的所有内容。

但问题是您无法从工作表中调用Get.Cell,但您可以从命名区域调用它。所以你必须按如下方式使用它

  1. 打开名称管理器
  2. 键入范围名称为Text_Align
  3. 在中键入此公式 =GET.CELL(8,OFFSET(ACTIVE.CELL(),0,-1))
  4. 点击确定
  5. 现在您在=Text_Align中键入的任何单元格,它将返回您输入公式的单元格左侧的单元格单元格,您可以修改上述公式以引用您需要的任何参考

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

        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.
    

答案 3 :(得分:0)

你可以这样做: = CELL("前缀",A1)

根据文档,前缀为您提供:

"前缀" 对应于"标签前缀的文本值"细胞如果单元格包含左对齐文本,则返回单引号(');如果单元格包含右对齐文本,则返回双引号(");如果单元格包含居中文本,则为插入符号(^);反斜杠( )如果单元格包含填充对齐的文本,并且如果单元格包含其他任何内容,则为空文本("")。

所以你的答案是:

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