如果单元格是公式,请选择列标题

时间:2016-06-02 14:23:15

标签: excel vba excel-vba

我已经找到了一些代码,但我无法得到的是如何解析该列,然后选择该列中的第一行。

如果所选单元格具有公式,则目标是为列的标题单元格着色。我正在寻找的那件作品是

Sub ColorFormulaHeaders()
Dim oWkbk As Workbook
Dim oWkst As Worksheet
Dim oRng As Range

For Each oRng In Selection.Cells
    If oRng.HasFormula Then
    'Select Column Header
         With Selection
            .Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent6
            .TintAndShade = 0
            .PatternTintAndShade = 0
         End With
    End If

End Sub

1 个答案:

答案 0 :(得分:4)

你有正确的想法。以下是访问列标题的一种方法:

Sub ColorFormulaHeaders()
    Dim oWkbk As Workbook
    Dim oWkst As Worksheet
    Dim oRng As Range

    For Each oRng In Selection.Cells
        If oRng.HasFormula Then
             With oRng.EntireColumn.Cells(1).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent6
                .TintAndShade = 0
                .PatternTintAndShade = 0
             End With
        End If
    Next oRng

End Sub

如果标签位于该单元格的 A 列中,您可以使用类似技术为某个单元格中的单元格获取行标签