VBA如何创build单元格底部边框每5行只有可见单元格

我正在寻找应用一个底部的边界,每隔五行可见。

循环从第14行开始,继续到第200行。

我想循环寻找单元格中的值(我,“D”)。 即列D中的每一行。

目前我得到一个对象所需的错误,我设置x = 0的行。我很困惑,因为我宣布x作为整数在顶部。

Sub Border() Dim i As Integer Dim x As Integer Dim sumsht As Worksheet Set sumsht = ThisWorkbook.Sheets("Sheet1") x = 0 For i = 14 To 200 x = sumsht.Cells(i, "D") + x If x = 5 Then With Worksheets("Sheet1").Rows(i).Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 1 End With Else End If Next i End Sub 

尝试这个。 你需要考虑行的高度。

也正如其他人所说,设置用于范围不只是variables(如你的X)。 你也使用sumsht但是不要在任何地方定义它。

 Option Explicit Sub add_lines() Dim rw, ct As Integer ct = 0 For rw = 14 To 200 If Rows(rw).Height <> 0 Then ct = ct + 1 End If If ct = 5 Then With Worksheets("Sheet1").Rows(rw).Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 1 End With ct = 0 End If Next rw End Sub 

删除其他边框使用(谢谢@Comintern):

 Range("A1:D22").Borders.LineStyle = xlLineStyleNone