gpt4 book ai didi

vba - 在使用的单元格范围VBA中应用边框

转载 作者:行者123 更新时间:2023-12-03 02:02:52 25 4
gpt4 key购买 nike

我正在尝试动态地在一组使用的单元格周围应用边框列范围为 (B7:E7) 数量将始终变化,因此代码需要是动态的。我的下面的代码没有实现这一点:

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 之外的所有非空单元格添加边框。

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(Range("B7"), Cells(lngLstRow, lngLstCol))
If rngCell.Value > "" Then
rngCell.Select 'Select cells
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
Next

Application.ScreenUpdating = True

End Sub

下面的代码在 B7 之外的使用范围周围添加边框:

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

关于vba - 在使用的单元格范围VBA中应用边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com