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

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

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

谢谢!

这将清除实际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之前,这是一个镜头:
在这里输入图像说明

而这个结果是:
在这里输入图像说明

我昨天试着发布这个post,但是无法通过“服务器忙于显示网页”和“发生错误发布了你的答案”。 这个答案不同于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 

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

因此,假设一个范围单元格(1,1)到单元格(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)