0

I need to autofit first two columns from Sheetx, but I want to be able to start MACRO from any other sheet so I'm using With-method. How to avoid using Select in this case?

Set ws = ThisWorkbook.Sheets("Sheetx")

With ws

    .Columns("A:B").Select

    Selection.EntireColumn.AutoFit

End with
YowE3K
  • 23,852
  • 7
  • 26
  • 40
user155754
  • 83
  • 3
  • 11

2 Answers2

2
Set ws = ThisWorkbook.Sheets("Sheetx")

With ws
    .Columns("A:B").AutoFit
End with

This might help you aswell.

Plagon
  • 2,689
  • 1
  • 11
  • 23
  • Nice Answer: Note: The statment `EntireColumn` is not needed her. Because he selects allready the hole Column. This statment is only needed when he Would Select `Range("A1:B1")` then he would ned the `EntireColumn` Statment. Here it does not matter, however, if you use it in a big loop, it causes an unnecessary lookup every time. – Moosli Sep 05 '17 at 07:58
2

Like this:

Set ws = ThisWorkbook.Sheets("Sheetx")

With ws
    .Columns("A:B").AutoFit
End with
Moosli
  • 3,140
  • 2
  • 19
  • 45