在使用的单元格范围VBA中应用边框
我试图在一组使用过的单元格周围动态应用边框。列的范围是B7:E7行的数量总是不同的,所以代码需要是动态的。我下面的代码无法实现这一点:在使用的单元格范围VBA中应用边框,vba,loops,excel,border,Vba,Loops,Excel,Border,我试图在一组使用过的单元格周围动态应用边框。列的范围是B7:E7行的数量总是不同的,所以代码需要是动态的。我下面的代码无法实现这一点: Sub Borders() Application.ScreenUpdating = False Dim lngLstCol As Long, lngLstRow As Long lngLstRow = ActiveSheet.UsedRange.Rows.Count lngLstCol = ActiveSheet.UsedRange.Columns.Cou
Sub Borders()
Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long
lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count
For Each rngCell In Range("B7:B" & lngLstRow)
If rngCell.Value > "" Then
r = rngCell.row
c = rngCell.Column
Range(Cells(r, c), Cells(r, lngLstCol)).Select
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
Next
Application.ScreenUpdating = True
End Sub
此代码在B7之外的所有非空单元格周围放置边框 下面的代码将使用范围的边框置于B7之外:
这将为列B:C中第6行下方的所有非空白单元格添加边框
Sub AddBorders()
Dim Rws As Long, Rng As Range, c As Range
Rws = Range("A1").SpecialCells(xlCellTypeLastCell).Row
Set Rng = Range(Cells(7, "B"), Cells(Rws, "C"))
For Each c In Rng.Cells
If c <> "" Then
With c.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
Next c
End Sub
代码将边框应用于B列为>的每一行。你到底期望什么?我希望能够在B7:C7列到lastrow列的用过的单元格周围应用boarder,所以我的代码是错误的,您可以添加预期输出样本的图像吗?此代码按预期在所有单元格B7和B7以外的单元格左右两侧创建边框。@user3964075例如,我想在范围B6:E12周围应用一块板。列范围没有改变,它是B6:E6,只是行数根据生成的数据而变化。“所以它基本上是一个厚框。”詹姆斯我编辑了我的代码。你也可以用这个。这更好,可能更快。这怎么可能是正确的答案?您要求在B:C列中输入边框,上面的代码将在第6行下面的所有单元格以及除第列以外的所有列上设置边框A@Davesexcel这已经在上面讨论过了。为这一混乱道歉。非常感谢。@Jamesı添加了一个代码,该代码将显示所使用的范围。当然,这与空单元格无关。@danieltakeshi你是对的;谢谢:不需要。
Sub BordersB()
Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long
lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count
With Range(Range("B7"), Cells(lngLstRow, 2)).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Range(Range("B7"), Cells(7, lngLstCol)).Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Range(Cells(7, lngLstCol), Cells(lngLstRow, lngLstCol)).Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Range(Cells(lngLstRow, 2), Cells(lngLstRow, lngLstCol)).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Application.ScreenUpdating = True
End Sub
Sub AddBorders()
Dim Rws As Long, Rng As Range, c As Range
Rws = Range("A1").SpecialCells(xlCellTypeLastCell).Row
Set Rng = Range(Cells(7, "B"), Cells(Rws, "C"))
For Each c In Rng.Cells
If c <> "" Then
With c.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
Next c
End Sub