通过 VBA 向最后使用的行添加边框?
Adding border to last used row via VBA?
我有一个代码,当 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,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
但是我的代码停止工作并且我得到这个错误
谁能告诉我哪里出错了?
您是否尝试明确设置其他边框?
.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
试试这个。它应该只为一系列单元格的顶部和底部着色。如果需要帮助,您可以将其调整为行,请告诉我。
Dim rRng As Range
Set rRng = Sheet1.Range("Z15:Z50000")
rRng.Select
rRng.Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
这就是您的代码应该看起来的样子
With Range("A9:P1048576")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression,Formula1:="=ROW(B9)=ROW(OFFSET($B,COUNTA($B:$B)-2,0))"
.Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Borders.Color = ThisWorkbook.Colors(3)
end with
我有一个代码,当 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,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
但是我的代码停止工作并且我得到这个错误
谁能告诉我哪里出错了?
您是否尝试明确设置其他边框?
.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
试试这个。它应该只为一系列单元格的顶部和底部着色。如果需要帮助,您可以将其调整为行,请告诉我。
Dim rRng As Range
Set rRng = Sheet1.Range("Z15:Z50000")
rRng.Select
rRng.Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
这就是您的代码应该看起来的样子
With Range("A9:P1048576")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression,Formula1:="=ROW(B9)=ROW(OFFSET($B,COUNTA($B:$B)-2,0))"
.Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
.Borders.Color = ThisWorkbook.Colors(3)
end with