3

I need to select columns on a specific sheet. Somehow this is not working:

Dim ws As Worksheet
Set ws = Worksheets("Mysheet")
ws.Columns("A:S").Select
Selection.EntireColumn.AutoFit

And simple Columns("A:S").Selectdoesn't activate the sheet I need

Totallama
  • 418
  • 3
  • 10
  • 26
  • Possible duplicate of other topics like [this one](http://stackoverflow.com/questions/17559677/select-range-in-a-particular-sheet-in-excel-vba). While I understand it can be difficult to formulate and search for a problem on Google, please have some attempt to look at possible previous answers. – Pierre Chevallier Nov 11 '16 at 12:26

1 Answers1

4

I tested your code and it works fine as follows.

Sub test()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Mysheet")
    ws.Columns("A:S").EntireColumn.AutoFit
End Sub

No need to Select anything, so I put the two statements together without the Select.

I added ThisWorkbook to (more) fully qualify your ws declaration. Make sure the worksheet Mysheet is in ThisWorkbook otherwise change that to state which workbook the sheet resides in.

CallumDA
  • 12,025
  • 6
  • 30
  • 52