我有一个代码,当B列中的单元格发生变化时会运行。
代码:
'Insert Depot Memo Data for user
Dim oCell As Range, targetCell As Range
Dim ws2 As Worksheet
If Not Intersect(Target, Range("B:B")) Is Nothing Then ' <-- run this code only if a value in column B has changed
If ActiveCell.Value <> "" Then
If Not GetWb("Depot Memo", ws2) Then Exit Sub
With ws2
For Each targetCell In Target
Set oCell = .Range("J1", .Cells(.Rows.Count, "J").End(xlUp)).Find(What:=targetCell.Value, LookIn:=xlValues, LookAt:=xlWhole)
If ActiveCell.Value <> "" Then
If Not oCell Is Nothing Then
Application.EnableEvents = False
'Set Format of cell
targetCell.ClearFormats
targetCell.Font.Name = "Arial"
targetCell.Font.Size = "10"
targetCell.Font.Color = RGB(128, 128, 128)
targetCell.HorizontalAlignment = xlCenter
targetCell.VerticalAlignment = xlCenter
targetCell.Borders(xlEdgeBottom).LineStyle = xlContinuous
targetCell.Borders(xlEdgeTop).LineStyle = xlContinuous
targetCell.Borders.Color = RGB(166, 166, 166)
targetCell.Borders.Weight = xlThin
targetCell.Offset(0, -1).Value = Now()
targetCell.Offset(0, 1).Value = oCell.Offset(0, 1)
targetCell.Offset(0, 2).Value = oCell.Offset(0, -2)
targetCell.Offset(0, 3).Value = oCell.Offset(0, -7)
With Range("A9:P1048576")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))"
With .FormatConditions(1).Borders
.LineStyle = xlContinuous
.Color = vbRed
.Weight = xlThin
End With
End With
Application.EnableEvents = True
End If
End If
Next
End With
End If
End If
大多数代码运行正常。 除非我想在单元格更改时将条件格式添加到范围。
这也有效,并在下一个可用(空行)周围添加红色边框。 但是,我不希望整个行周围有红色边框。我只想要顶部和底部边框。
所以我想这样做:
With .FormatConditions(1).Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Color = vbRed
.Weight = xlThin
End With
然后我的代码停止工作,我收到此错误
请有人告诉我我哪里出错了吗?
答案 0 :(得分:0)
您是否尝试明确设置其他边框?
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = vbRed
.TintAndShade = 0
.Weight = xlThin
End With
答案 1 :(得分:0)
试试这个。它应该只对一系列细胞的顶部和底部着色。您可以根据行进行调整,如果您需要帮助,请告诉我。
Dim rRng As Range
Set rRng = Sheet1.Range("Z15:Z50000")
rRng.Select
rRng.Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
答案 2 :(得分:0)
这是你的代码看起来的样子
With Range("A9:P1048576")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression,Formula1:="=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))"
.Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Borders.Color = ThisWorkbook.Colors(3)
end with