(Solved) – Range fails if sheet is not selected

  • by
(solved)-–-range-fails-if-sheet-is-not-selected

I have this code which should work fine

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Auswertung")
...
ws.Range(Cells(2, 2), Cells(lastRow, lastCol)).NumberFormat = "0"

It however fails if the sheet is not selected. Error 1004, Methode Range fails for the object.

If I change it to

ws.Select
ws.Range(Cells(2, 2), Cells(lastRow, lastCol)).NumberFormat = "0"

it works.

why? In VBA I want to avoid select.

Leave a Reply

Your email address will not be published. Required fields are marked *