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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,215,379
Messages
6,124,608
Members
449,174
Latest member
ExcelfromGermany

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