Tribias
Tribias

Reputation: 79

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

I am trying to add all borders to the contents below the headers that I have. The range would be A7 to Ox where x is the last row of content. The first part of the code listed looks for CFS-GHOST-DJKT and deletes the row which works perfectly. I am unsure about how to select the lower ending row correctly.

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

Upvotes: 0

Views: 1687

Answers (1)

YowE3K
YowE3K

Reputation: 23974

Using a term of Range(A7, Ox) is telling VBA to select the rectangular area defined by the Address of the variable (of type Range) A7 as one corner and the Address of the variable (also of type Range) Ox as the other corner.

As you have defined neither of those variables, your code fails.

Try this instead:

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

Upvotes: 0

Related Questions