我写了一些代码来填充标题的内部颜色,只在我使用的范围内的最后一行。它工作得很好,但我觉得必须有一种更优雅的方式来做这件事,而不是拥有如此庞大的代码块。
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或类似的东西?
发布于 2019-11-12 23:47:48
由于列没有真正的模式(据我所知),您需要将它们设置为循环或使其唯一。
在您的例子中,命名您想要更改的列可能是有意义的。然后使用intersect/usedrange在一个运动中进行更改。
示例:
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 Subhttps://stackoverflow.com/questions/58821696
复制相似问题