2017-04-22 60 views
-1

我试图创建代码以查找在包含0,那么这将格式化直接跌破8个细胞有一个白色的背景与白色字体特定列细​​胞的细胞。基本上使细胞不可见。理想情况下,我希望能够将8个单元格更改为原始格式,如果搜索单元格中有X.有任何帮助? -ThanksVBA的Excel - 找到一个细胞和格式化以下

Sample Data

可惜我一个新的用户,但无法显示的示例图像,请点击上面的链接。

+0

你“在特定的行”之说,但没有告诉我们如何识别它们。代码如何知道从哪里开始? –

+1

请分享您迄今为止编写的代码。 –

+0

尽管我在下面提供了一个答案,但对我而言,您可以使用条件格式来做到这一点。 –

回答

0

你还没有回答我们的问题,所以下面可能没你想要的...但它应该是一个良好的开端。

Sub Hide8CellsBelow0() 

    Dim arrayRowNumbers() As Variant 
    arrayRowNumbers = Array(2, 12) ' <-- "Specific Rows" 

    Dim intRow As Integer 
    Dim objCell As Range 

    For intRow = 0 To UBound(arrayRowNumbers) 

     For Each objCell In ThisWorkbook.ActiveSheet.Rows(arrayRowNumbers(intRow)).Cells 

      Debug.Print objCell.Address & " : " & objCell.Value 

      If objCell.Text = "" Then Exit For ' <-- Quit the row after finding an empty cell 
      If objCell.Value = 0 Then 

       With Range(objCell.Offset(1), objCell.Offset(8)) 
        'I got the following from recording a Macro, you don't have to remember everything 
        With .Interior 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
        .ThemeColor = xlThemeColorDark1 
        .TintAndShade = 0 
        .PatternTintAndShade = 0 
        End With 
        With .Font 
        .ThemeColor = xlThemeColorDark1 
        .TintAndShade = 0 
        End With 
       End With 

      End If 

     Next objCell 

    Next intRow 

End Sub 
0

谢谢Steve!我对过去几个小时没有回应表示歉意。我和家人一起在地球日清理。

我改变了它稍微看一下这是8,18和28“特定行”,它按预期工作。然后,我添加了第二个宏更改字体回到以前如果X存在于行,而不是0

Sub Hide8CellsBelow0() 

Dim arrayRowNumbers() As Variant 
arrayRowNumbers = Array(8, 18, 28) ' <-- "Specific Rows" 

Dim intRow As Integer 
Dim objCell As Range 

For intRow = 0 To UBound(arrayRowNumbers) 

    For Each objCell In ThisWorkbook.ActiveSheet.Rows(arrayRowNumbers(intRow)).Cells 

     Debug.Print objCell.Address & " : " & objCell.Value 

     If objCell.Text = "" Then Exit For ' <-- Quit the row after finding an empty cell 
     If objCell.Value = 0 Then 

      With Range(objCell.Offset(1), objCell.Offset(8)) 
       'I got the following from recording a Macro, you don't have to remember everything 
       With .Interior 
       .Pattern = xlSolid 
       .PatternColorIndex = xlAutomatic 
       .ThemeColor = xlThemeColorDark1 
       .TintAndShade = 0 
       .PatternTintAndShade = 0 
       End With 
       With .Font 
       .ThemeColor = xlThemeColorDark1 
       .TintAndShade = 0 
       End With 
      End With 

     End If 

    Next objCell 

Next intRow 

End Sub 

Sub Show8CellsBelowX() 

Dim arrayRowNumbers() As Variant 
arrayRowNumbers = Array(8, 18, 28) ' <-- "Specific Rows" 

Dim intRow As Integer 
Dim objCell As Range 

For intRow = 0 To UBound(arrayRowNumbers) 

    For Each objCell In ThisWorkbook.ActiveSheet.Rows(arrayRowNumbers(intRow)).Cells 

     Debug.Print objCell.Address & " : " & objCell.Value 

     If objCell.Text = "" Then Exit For ' <-- Quit the row after finding an empty cell 
     If objCell.Value = "X" Then 

      With Range(objCell.Offset(1), objCell.Offset(1)) 
       With .Interior 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
        .Color = 65535 
        .TintAndShade = 0 
        .PatternTintAndShade = 0 
       End With 
       With .Font 
        .Color = -16776961 
        .TintAndShade = 0 
       End With 
      End With 

      With Range(objCell.Offset(2), objCell.Offset(2)) 
       With .Font 
        .ColorIndex = xlAutomatic 
        .TintAndShade = 0 
       End With 
      End With 

      With Range(objCell.Offset(3), objCell.Offset(3)) 
       With .Interior 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
        .Color = 10079487 
        .TintAndShade = 0 
        .PatternTintAndShade = 0 
       End With 
       With .Font 
        .Color = -16776961 
        .TintAndShade = 0 
       End With 
      End With 

      With Range(objCell.Offset(4), objCell.Offset(4)) 
       With .Interior 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
        .Color = 13434828 
        .TintAndShade = 0 
        .PatternTintAndShade = 0 
       End With 
       With .Font 
        .Color = -16776961 
        .TintAndShade = 0 
       End With 
      End With 

      With Range(objCell.Offset(5), objCell.Offset(6)) 
       With .Font 
        .ColorIndex = xlAutomatic 
        .TintAndShade = 0 
       End With 
      End With 

      With Range(objCell.Offset(7), objCell.Offset(8)) 
       With .Font 
        .Color = -16776961 
        .TintAndShade = 0 
       End With 

      End With 

     End If 

    Next objCell 

Next intRow 

End Sub