0

I get an "application-defined or object-defined error" remark on the following line when I want to select a range of cells:

Sheets("sheet").Range(Cells(1, 1), Cells(k, 27)).Borders(xlDiagonalDown).LineStyle = xlNone

Why does that not work, but the following line does?

Sheets("sheet").Activate
Range(Cells(1, 1), Cells(k, 27)).Borders(xlDiagonalDown).LineStyle = xlNone

or this line also works:

Sheets("sheet").Cells(1,1).Borders(xlDiagonalDown).LineStyle = xlNone

editted for more clearance

Ry-
  • 218,210
  • 55
  • 464
  • 476
Ezraneut
  • 1
  • 6
  • 1
    Looks like your sheet `sheet` might not be activated in the first example. However, please not that you normally don't need to select a range - see [this post](http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select)! – Peter Albert Dec 19 '13 at 14:17
  • I tried the activate to see if that was a solution and it appeared to be so, but earlier in the code i also do: "Sheets("sheet").Cells(k + 1, 1).Value = tags(k).tagname" without activating the sheet. Why does that work then? – Ezraneut Dec 19 '13 at 14:22
  • The selecting was just an example. I could do anything on the fields directly, ie. changing the font – Ezraneut Dec 19 '13 at 14:24
  • 1
    the `.Select` method requires you to have the sheet activated. You can manipulate values on the sheet without activating it - but to Select anything from the sheet it needs to be active/selected. –  Dec 19 '13 at 14:24
  • 1
    Working with ranges does not required the sheet to be active. however, a selection can only be - by definition - on the active sheet, therefore you run into the error. – Peter Albert Dec 19 '13 at 14:26
  • the actual line that fails is: "Sheets("sheet").Range(Cells(1, 1), Cells(k, 27)).Borders(xlDiagonalDown).LineStyle = xlNone – Ezraneut Dec 19 '13 at 14:27
  • I will edit this in the OP – Ezraneut Dec 19 '13 at 14:38

1 Answers1

0

Your syntax is off. Give this a try:

Range(Sheets("sheet").Cells(1, 1), Sheets("sheet").Cells(k, 27)).Borders(xlDiagonalDown).LineStyle = xlNone
sous2817
  • 3,915
  • 2
  • 33
  • 34