VBA如何创建单元格底部边界每5行仅可见的单元格



我希望将底部边框应用到每5个可见行。

循环从第14行开始,一直到第200行。

我希望循环查找单元格(I, "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

试试这个。你需要考虑行高。

也正如其他人所说,set不仅仅用于变量(比如你的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

最新更新