First of all I recommend to read How to avoid using Select in Excel VBA which is absolutely necessary to follow strictly if you are a beginner. This makes your code a lot faster and cleaner.
Yes I know this is a recorded macro, and yes they always have a lot of .Select
. This is why you should always re-write your recordings to get a good clean and reliable code. But recordings are good to have something to start with.
Then it is better to specify a worksheet by its name and avoid ActiveSheet
where ever you can.
Also always specify a worksheet for every Range()
or Cells()
etc. Otherwise Excel guesses which worksheet you mean and Excel might guess something different from your guess and then fails.
So the code you tried would better look like this …
Option Explicit
Public Sub DoMyStuff()
Dim wsSrc As Worksheet
Set wsSrc = ActiveSheet 'better specify sheet by name: Set wsSrc = Worksheets("SheetName")
Dim wsNew As Worksheet
Set wsNew = Workbooks.Add().Worksheets(1) 'get first worksheet of a new added workbook
wsSrc.Range("$A$3:$L$10001").AutoFilter Field:=6, Criteria1:="O/S"
wsSrc.Columns("A:E").Copy wsNew.Range("A1") 'copy from source sheet directly into the new sheet
wsNew.Cells.EntireColumn.AutoFit
wsNew.Rows("1:1").RowHeight = 89.25
End Sub