在整个Excel电子表格中将彩色单元格转换为无填充单元格

时间:2015-12-04 14:37:17

标签: excel vba excel-vba

这是我的代码似乎不起作用。我基本上在第3行“对于UsedRange.Cells中的每个单元格”中出现“需要对象”错误

#MyTable

3 个答案:

答案 0 :(得分:1)

基本If块的解剖结构:

If [condition] = [true | false] Then
    '// Do something
[Else]
    ['// Do something Else]
End If

所以在你的情况下,你会想要这样的东西:

(在评论反馈后修改)

Dim myRange As Range

For Each cell In UsedRange.Cells
    If cell.Interior.ColorIndex = 44 Then
        If myRange Is Nothing Then
            Set myRange = cell
        Else
            Set myRange = Union(myRange, cell)
        End If
    End If
Next 

If Not myRange Is Nothing Then
    myRange.Interior.ColorIndex = xlNone
    Range("P7").ClearContents
    Columns("E:F").EntireColumn.Delete
End If

答案 1 :(得分:1)

从未通过手动设置背景填充或是否由条件格式设置规则填充来确定单元格是否为橙色填充。如果是后者,那么您可以将Interior.Pattern property设置为白色或 xlNone 。 CF规则将覆盖您设置的任何内容。您可以清除内容(我假设驱动CF规则)或从该单元格中删除CF规则。

AutoFilter method可以过滤Range.DisplayFormat property。 .DisplayFormat包括常规格式和条件格式;即如果你看到橙色,那么也是如此.DisplayFormat。

Sub AgreeAll()
    Dim rng As Range
    Dim c As Long

    'Application.ScreenUpdating = False

    With Worksheets("Sheet2") '<~~set this worksheet reference properly!
        'why wait until the end to do this? It only means you have to process rtwo columns you plan to delete
        .Columns("E:F").EntireColumn.Delete
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            For c = 1 To .Columns.Count
                With .Columns(c)
                    .AutoFilter Field:=1, Criteria1:=RGB(255, 192, 0), _
                                Operator:=xlFilterCellColor
                    If .Cells.SpecialCells(xlCellTypeVisible).Count > 1 Then
                        .Offset(1, 0).Interior.Pattern = xlNone
                        For Each rng In .SpecialCells(xlCellTypeVisible)
                            If rng.DisplayFormat.Interior.Color = RGB(255, 192, 0) Then _
                                rng.FormatConditions.Delete
                        Next rng
                        'your code cleared P7; did you want to clear all of the orange cells?
                        '.Offset(1, 0).ClearContents
                    End If
                    .AutoFilter
                End With
            Next c
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
    End With

    Application.ScreenUpdating = True

End Sub

你的代码和叙述都没有充分解释Range("P7").ClearContents应该做什么。如果你想清除橙色单元格,那么我在相应的位置留下了一些注释代码来做到这一点。

如果您遇到问题,请随时发表评论,但请记住提供足够的信息,以便我帮助您。

答案 2 :(得分:0)

试试这个:

Sub AgreeAll()
Dim wS As Worksheet, _
    myRange As Range, _
    aCell As Range

Set wS = ActiveSheet
'set ws =sheets("Your_Sheet's_Name")

With wS
    For Each aCell In .UsedRange.Cells
        If aCell.Interior.Color = RGB(255, 192, 0) Then
            If myRange Is Nothing Then
                Set myRange = aCell
            Else
                Set myRange = Union(myRange, aCell)
            End If
        End If
    Next

    If Not myRange Is Nothing Then
        myRange.Interior.Pattern = xlNone
        .Range("P7").ClearContents
        .Columns("E:F").EntireColumn.Delete
    End If
End With

End Sub