Autofit and Autofilter in Excel VBA - Revision Needed on my code?

gertie193

Board Regular
Joined
Feb 28, 2008
Messages
82
Hello,

I am learning VBA and have a large code that formats an Excel spreadsheet. The code works, but I just want to see if there is a better way to write the code of Autofit and Autofilter than what I have below for the sake of learning. I ask, too, b/c I hear some people say to not use Select in code, and I didn't know if I was doing that in the codes below.

Code:
'turn on autofilter
Range("A1").AutoFilter

I tried to use other code to just apply autofilter by recognizing the top row of the used range, but it caused problems and another code would add filter to the entire row in Excel.

Code:
'autofit columns
Cells.EntireColumn.AutoFit

This is what code came up when I created a macro to autofit. I removed the Cell.Select part that was at the beginning b/c, as I mentioned, I've heard you shouldn't use Select. Will this cause any problems by being too generic or anything?

Thank you.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top