VBA代码使用相交来选择单元格并应用条件格式

时间:2014-12-08 05:02:31

标签: excel vba excel-vba

我正在寻找解决方案来解决限制因素,如果它包含合并的单元格,则无法正确选择(突出显示)列和行。不幸的是,由于格式化的丢失,我无法取消单元格;因此,我希望创建一个条件格式规则,该规则仅适用于“交叉”和“交叉”。细胞。然后,我可以使用此子句中的条件格式,并根据需要将其删除。

下面是用于选择(突出显示)交叉列和行(未合并)的工作代码。我希望有人知道一种创建条件格式规则的方法,该规则只需将匹配的兴趣单元格背景设置为绿色。任何想法都会很棒? : - )

Sub previewSub()
   Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        Call previewDisplayColumnRow(ws)

    Next
        MsgBox "Click on each Worksheets to preview columns/rows which will be displayed after clicking on 'UPDATE LAYOUT'"
End Sub

Sub previewDisplayColumnRow(ws As Worksheet)

    Dim DisplayColumns       As Variant
    Dim DisplayRows          As Variant
    Dim HideColumnsRows      As Variant
    Dim SelectColumnsRows    As String
    Dim myrange

    Set myrange = Worksheets("Settings").Range("range_sheetProperties")

    'Lookup Worksheet name and identify columns & rows to display
    HideColumnsRows = Application.VLookup(ws.Name, myrange, 6, False)

    If Not IsError(HideColumnsRows) Then 'If no error.

        DisplayColumns = Application.VLookup(ws.Name, myrange, 7, False)
        DisplayRows = Application.VLookup(ws.Name, myrange, 8, False)

        SelectColumnsRows = DisplayColumns & "," & DisplayRows

       'Selects Columns / Rows which will be hidden
        If HideColumnsRows = "Y" Then
            'MsgBox ws.Name & " - " & SelectColumnsRows
            ws.Activate
            Intersect(ws.Range(DisplayColumns), ws.Range(DisplayRows)).Select
        End If

    End If
End Sub

1 个答案:

答案 0 :(得分:0)

对于那些需要未来的人,我得到了这个工作。见下文!

Sub previewSub()
   Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        Call previewDisplayColumnRow(ws)

    Next
        Sheets("Settings").Activate
        MsgBox "Click on each Worksheets to preview columns/rows which will be displayed after clicking on 'UPDATE LAYOUT'"
End Sub

Sub previewDisplayColumnRow(ws As Worksheet)

    Dim DisplayColumns       As Variant
    Dim DisplayRows          As Variant
    Dim HideColumnsRows      As Variant
    Dim SelectColumnsRows    As String
    Dim myrange

    Set myrange = Worksheets("Settings").Range("range_sheetProperties")

    'Lookup Worksheet name and identify columns & rows to display
    HideColumnsRows = Application.VLookup(ws.Name, myrange, 6, False)

    If Not IsError(HideColumnsRows) Then 'If no error.

        DisplayColumns = Application.VLookup(ws.Name, myrange, 7, False)
        DisplayRows = Application.VLookup(ws.Name, myrange, 8, False)

        SelectColumnsRows = DisplayColumns & "," & DisplayRows

       'Selects Columns / Rows which will be hidden
        If HideColumnsRows = "Y" Then
            'MsgBox ws.Name & " - " & SelectColumnsRows
            ws.Activate
            Cells.FormatConditions.Delete
            ws.Columns.EntireColumn.Hidden = False
            ws.Columns.EntireRow.Hidden = False

            With Intersect(ws.Range(DisplayColumns), ws.Range(DisplayRows))
                .FormatConditions.Add Type:=xlExpression, Formula1:="=$A$1<>""WHOs GOING TO TYPE THIS NOBODY"""
                .FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
                .FormatConditions(1).Interior.Color = 13561798
                .FormatConditions(1).Interior.TintAndShade = 0
            End With
        End If
    End If
End Sub