4
Private Sub Workbook_Open()
Dim WB As Workbook

Set WB = ActiveWorkbook


WB.Sheets("Automation").Range("U23:W467").Select

Selection.ClearContents

End Sub

From what I can gather from SO, this code should automatically clear contents in those cells when I open the workbook. However, it doesn't. When I step into the code everything works fine and the cells are cleared.

Any help is appreciated!

Daruki
  • 481
  • 3
  • 8
  • 18
  • 1
    Seems legit to me. Do you have security against macros? – gazzz0x2z Dec 08 '15 at 18:04
  • I went to macro security and checked enable all macros, still nada – Daruki Dec 08 '15 at 18:07
  • 2
    Quick tip, you can [avoid using `.Select`](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) by just combining your two lines to `WB.Sheets("Automation").Range("U23:W467").ClearContents`. This should work though, are you perhaps in a read-only mode? Or have something preventing macros from being run? When you say it doesn't clear the contents, what happens? Try removing `.Select` and see if that does it. – BruceWayne Dec 08 '15 at 18:07
  • 1
    I'm not using range because these are merged cells, select works better. When I say it doesn't clear the contents, I mean when I open the workbook, the cells still have info on it – Daruki Dec 08 '15 at 18:11
  • Really? Using `.Select` works better for merged cells? Interesting. I've always avoided Merged Cells, since they can be a headache, and that's another reason for me to avoid them. Thanks for that info! – BruceWayne Dec 08 '15 at 18:13

1 Answers1

7

Where is the code for sub Workbook_Open() placed?

If you put it in the ThisWorkbook class it should run.

If you have it in a regular code module it will not run with that name, but you could call the sub Auto_Open() instead and it should run automatically.

Olle Sjögren
  • 5,315
  • 3
  • 31
  • 51