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

时间:2016-11-06 19:28:21

标签: excel vba excel-vba border

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

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

1 个答案:

答案 0 :(得分:0)

使用Range(A7, Ox)这个术语告诉VBA选择变量Address定义的矩形区域(类型RangeA7作为一个角落变量Address(同样类型为RangeOx作为另一个角落。

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

请改为尝试:

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