如何在VBA for Excel中清除UsedRange之外的所有格式?

时间:2014-01-16 19:16:44

标签: excel vba excel-vba

我使用Cells(1,1).EntireRow.InteriorColor.Color突出显示整行,现在我需要清除UsedRange范围之外的所有内容的格式。有没有办法做到这一点?

每当我google,“超出范围”,我只会获得数组越界错误的链接。任何帮助表示赞赏。

谢谢!

3 个答案:

答案 0 :(得分:3)

这将清除实际WorkSheet的使用范围之外的格式:

Sub clearFormatOutsideUsedRange()

    Dim calcState As XlCalculation
    calcState = Application.Calculation
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim UpperRng As Range
    Dim LowerRng As Range
    Dim j As Long
    Dim startRow As Integer
    For j = 1 To Me.Columns.Count
        If Me.Cells(1, j).Value = "" Then
            Set UpperRng = Me.Range(Me.Cells(1, j), _
                              Me.Cells(1, j).End(xlDown).Offset(-1, 0))
            UpperRng.ClearFormats
        End If

        If Me.Cells(Me.Rows.Count, j).Value = "" Then
            Set LowerRng = Me.Range(Me.Cells(Me.Rows.Count, j).End(xlUp).Offset(1, 0), _
                              Me.Cells(Me.Rows.Count, j))
            LowerRng.ClearFormats
        End If
    Next j

    Application.ScreenUpdating = True
    Application.Calculation = calcState

End Sub

这是在运行marco之前的一个镜头:
enter image description here

这就是结果:
enter image description here

答案 1 :(得分:0)

我昨晚尝试发布此内容,但无法通过“服务器忙于显示网页”和“发布错误信息”。这个答案与SimpLE Man's不同,所以今天我会尝试发帖。

Option Explicit
Sub ClearFormattedCellsOutsideRangeWithValues()

  Dim ColLastUsed As Long
  Dim ColLastWithValue As Long
  Dim RowLastUsed As Long
  Dim RowLastWithValue As Long
  Dim RngUsed As Range


  With Sheets("Sheet1")
    RowLastWithValue = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row
    ColLastWithValue = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column

    ' Calculate last row and column allowing for top rows and left columns being unused
    ColLastUsed = .UsedRange.Columns.Count + .UsedRange.Column - 1
    RowLastUsed = .UsedRange.Rows.Count + .UsedRange.Row - 1

    Debug.Print "ColLastWithValue " & ColLastWithValue
    Debug.Print "ColLastUsed " & ColLastUsed
    Debug.Print "RowLastWithValue " & RowLastWithValue
    Debug.Print "RowLastUsed " & RowLastUsed

    If ColLastUsed > ColLastWithValue Then
      .Columns(ColNumToCode(ColLastWithValue + 1) & ":" & _
               ColNumToCode(ColLastUsed)).EntireColumn.Delete
    End If

    If RowLastUsed > RowLastWithValue Then
      .Rows(RowLastWithValue + 1 & ":" & RowLastUsed).EntireRow.Delete
    End If

  End With
End Sub
Function ColNumToCode(ByVal ColNum As Long) As String

  Dim Code As String
  Dim PartNum As Long

  ' Last updated 3 Feb 12.  Adapted to handle three character codes.
  If ColNum = 0 Then
    ColNumToCode = "0"
  Else
    Code = ""
    Do While ColNum > 0
      PartNum = (ColNum - 1) Mod 26
      Code = Chr(65 + PartNum) & Code
      ColNum = (ColNum - PartNum - 1) \ 26
    Loop
  End If

  ColNumToCode = Code

End Function

答案 2 :(得分:0)

更简单的方法是使用选择范围之外的命名范围。

因此,假设Cell(1,1)到Cell(rowActiveNum,colActiveNum)的范围,以下将清除所需数据之外的所有格式。

    rowNum = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
    colNum = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column

    'Remove any excess columns rows that are no longer used

    referStr = "=OFFSET('Source'!$A$1," & rowNum + 1 & ", 0, 1000000 - rowNum, 16300)"
    ActiveWorkbook.Names.Add Name:="SOURCE_EMPTY_ROW", RefersTo:=referStr
    ActiveSheet.Range("SOURCE_EMPTY_ROW").Delete

    referStr = "=OFFSET('Source'!$A$1, 0, " & colNum + 1 & ", 1000000, 16300 - colNum)"
    ActiveWorkbook.Names.Add Name:="SOURCE_EMPTY_COL", RefersTo:=referStr
    ActiveSheet.Range("SOURCE_EMPTY_COL").Delete

适用于Excel 2007.可用列总数约为16300(2 ^ 14),可用行总数约为1000000(2 ^ 20)