0

I am trying to add borders to a certain range in excel. Because I eventually want to use this code in a For loop, I want to define the Range using Cells().

This code does not work:

With ThisWorkbook.Worksheets("Data").Range(Cells(1, 1), Cells(1, 3)).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 1
End With

But this code (that refers to the same exact range) does not:

With ThisWorkbook.Worksheets("Data").Range("A1:C1").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 1
End With

What is going on here?

The error I get is

"Run-time error '1004': Application-defined or object-defined error".

BigBen
  • 46,229
  • 7
  • 24
  • 40
user7758051
  • 304
  • 2
  • 5
  • 18
  • 3
    You need to qualify which `Worksheet` the `Cells` are on, otherwise there's an implicit `ActiveSheet` here. When the "Data" sheet is not active, this throws an error. – BigBen Aug 26 '19 at 19:57
  • 1
    I would make your `With` block refer to the sheet. Then you can just qualify all downstream objects (ranges and cells) with block – urdearboy Aug 26 '19 at 19:58

0 Answers0