2016-10-20 125 views
-1

我有一个工作表,其中包含大量的工作表,并且每个包含一些具有特定背景颜色的行 是否可以删除具有特定颜色的所有单元格或行的背景颜色(黄色在工作表中的我的情况)的背景删除背景颜色,如果颜色是黄色excel

here is an example file

+0

更好的选择没有VBA与查找和替换https://www.extendoffice.com/documents/excel/2746-excel-find-and-replace-fill-color.html#a1 – Slai

+0

是的Slai,它的工作,由远远好于vba选项 – khalil

回答

0

是的,它是可能的。您必须检查特定范围的Range.Interior.Color值。在VBA中,这看起来是这样的:

If ActiveWorksheet.Range("A1").Interior.Color = RGB(255,255,0) Then 
    'Do something 
End If 

RGB(255,255,0)在这种情况下是黄色的。您也可以使用其他所有值。见RGB Function。要删除的颜色使用这样的:

Range("A1").Interior.Pattern = xlNone 

如果您需要进一步的帮助,请提供更详细的信息,关于您的问题和/或发表评论。

您可能还想看看这些链接:Range.InteriorInterior.Color

编辑:根据您的示例数据,您可能想要从列“E”,“F”,“G”和“H”中的各个工作表中删除绿色着色。绿色的RGB是RGB(146,208,80)。此代码应该做的工作:

Sub removeColor() 
    Dim lastRow As Long 
    Dim ws As Worksheet 
    Dim i As Long 
    Dim color As Long 
    color = RGB(146, 208, 80) 
    For Each ws In ThisWorkbook.Worksheets 
     lastRow = ws.Range("A65536").End(xlUp).Row 
     For i = 3 To lastRow 
      If ws.Range("E" & i).Interior.color = color Then 
       ws.Range("E" & i).Interior.Pattern = xlNone 
      End If 
      If ws.Range("F" & i).Interior.color = color Then 
       ws.Range("F" & i).Interior.Pattern = xlNone 
      End If 
      If ws.Range("G" & i).Interior.color = color Then 
       ws.Range("G" & i).Interior.Pattern = xlNone 
      End If 
      If ws.Range("H" & i).Interior.color = color Then 
       ws.Range("H" & i).Interior.Pattern = xlNone 
      End If 
     Next i 
    Next ws 
End Sub 

编辑:如果你要检查所有的细胞来进行颜色使用:

Sub removeColor() 
    Dim ws As Worksheet 
    Dim cell as Range 
    Dim color As Long 
    color = RGB(146, 208, 80) 
    For Each ws In ThisWorkbook.Worksheets 
     For Each cell in ws.UsedRange 
      If cell.Interior.Color = color Then 
       cell.Interior.Pattern = xlNone 
      End If 
     Next cell 
    Next ws 
End Sub 
+0

感谢Fabian F,这里是一个示例xlsx文件的链接,我想从单元格中删除绿色背景[http://www.filedropper.com/example_9] – khalil

+0

请查看编辑示例文件 – khalil

+0

谢谢,它适用于我的示例,但如果我想遍历所有单元格,因为其他单元格可以着色? – khalil

0

这可能会解决您选择的问题:

For i = 1 To Sheets.Count 
    On Error GoTo nex: 
    Sheets(i).Activate 
    For j = 1 To ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row 
     For k = 1 To ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, SearchDirection:=xlPrevious).Column 
      '65535 equals yellow 
      '5296274 equals green 
      If ActiveSheet.Cells(j, k).Interior.Color = 65535 Then ActiveSheet.Cells(j, k).Interior.Pattern = xlNone 
     Next 
    Next 
nex: 
Next 

的循环遍历所有工作表并在所有使用的单元格中查找背景色。

要获得代表一种颜色,你可以只是颜色A1中所需的颜色和运行下面的代码数量:

MsgBox (ActiveSheet.Cells(1, 1).Interior.Color) 

希望我能帮上忙。

+0

这段代码不起作用,它会将我发送到第三张表 – khalil

+0

代码将通过所有表单激活它们,因此在代码完成后,您将在最后一张表上。 – FatTony

+0

如果你想要替换绿色的颜色(就像你在示例文件中),你需要改变'If ActiveSheet.Cells(j,k).Interior.Color = 65535然后'因为65535会适合黄色。 – FatTony