填充标题内部颜色到使用范围的最后一行?

时间:2019-11-12 15:24:07

标签: excel vba

我编写了一些代码,以便将标头内部颜色仅填充到我使用范围内的最后一行。它工作正常,但是我觉得必须有一种更优雅的方法来做到这一点,而不是拥有大量的代码块。

Option Explicit

Sub FillColors()

    Dim wb As Workbook
    Set wb = Workbooks("Template.xlsx")
    Dim lastrow As Long
    Dim rng As range



    With wb.Sheets("File")

    lastrow = .range("A" & .Rows.Count).End(xlUp).Row
    .range("AH3:AI" & lastrow).Interior.Color = .range("AH2:AI2").Interior.Color
    .range("AJ3:AL" & lastrow).Interior.Color = .range("AJ2:AL2").Interior.Color
    .range("AM3:AN" & lastrow).Interior.Color = .range("AM2:AN2").Interior.Color
    .range("AO3:AO" & lastrow).Interior.Color = .range("AO2").Interior.Color
    .range("AX3:AX" & lastrow).Interior.Color = .range("AX2").Interior.Color
    .range("AY3:AY" & lastrow).Interior.Color = .range("AY2").Interior.Color
    .range("BC3:BD" & lastrow).Interior.Color = .range("BC2:BD2").Interior.Color
    .range("BF3:BF" & lastrow).Interior.Color = .range("BF2").Interior.Color
    .range("BG3:BG" & lastrow).Interior.Color = .range("BG2").Interior.Color
    .range("BK3:BL" & lastrow).Interior.Color = .range("BK2:BL2").Interior.Color

    Set rng = .range("A3:BP" & lastrow)

        With rng.Borders
            .LineStyle = xlContinuous
            .Color = vbBlack
            .Weight = xlThin
        End With

    End With



End Sub

是否可以通过实现if cell.interior <> blank Then filldown interior或类似的方法来实现此目的?

1 个答案:

答案 0 :(得分:0)

由于列(我可以看到)没有真正的模式,因此您需要进行一些设置以使其循环或使其唯一。

对于您而言,命名要更改的列可能很有意义。然后使用相交/使用范围一次更改动作。

示例:

Sub quickExample()
Const theNamedRanged As String = "Bang"

    Dim ws As Worksheet, changrange As Range
    Set ws = Sheets("file")

    Set changrange = Intersect(ws.Range("A2:zz999999"), ws.Range(theNamedRanged), ws.UsedRange)

    'then do what you need with the range.
    changerange.Interior.Color = xlBOOM

End Sub