尝试将所有边框添加到 excel vba 中具有可变行数的范围

Trying to add all borders to range with variable row count in excel vba

我正在尝试将所有边框添加到我拥有的 headers 下方的内容。范围是 A7 到 Ox,其中 x 是内容的最后一行。列出的代码的第一部分查找 CFS-GHOST-DJKT 并删除完美运行的行。我不确定如何正确 select 下端行。

Dim x As Long
For x = Cells(Rows.Count, "A").End(xlUp).Row To 7 Step -1
    If Cells(x, "A") = "CFS-GHOST-DJKT" Then Rows(x).Delete


'Add Gridlines=========================================================


      Range(A7, Ox).Select
      With Selection.Borders
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin

      End With

使用术语 Range(A7, Ox) 告诉 VBA 到 select 由变量的 Address 定义的矩形区域(Range 类型) A7 作为一个角,变量的 Address(也是 Range 类型)Ox 作为另一个角。

由于您没有定义这两个变量,因此您的代码失败。

试试这个:

Dim x As Long
For x = Cells(Rows.Count, "A").End(xlUp).Row To 7 Step -1
    If Cells(x, "A") = "CFS-GHOST-DJKT" Then Rows(x).Delete
Next

With Range("A7:O" & Cells(Rows.Count, "A").End(xlUp).Row).Borders
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With