I have the following working code as part of a bigger function
Dim SheetToModify As Worksheet
Set SheetToModify = Sheets(strNewProjSheetName)
Dim x As Integer
For x = 1 To SheetToModify.UsedRange.Columns.Count
SheetToModify.Columns(x).EntireColumn.AutoFit
Next x
Where strNewProjSheetName is a string.
I'd like to extract the for loop in to a function. To do this I have
Public Function AutofitAllUsedColumns(mySheet As Worksheet)
Dim x As Integer
For x = 1 To mySheet.UsedRange.Columns.Count
mySheet.Columns(x).EntireColumn.AutoFit
Next x
End Function
and call it with the following
Dim SheetToModify As Worksheet
Set SheetToModify = Sheets(strNewProjSheetName)
AutofitAllUsedColumns (SheetToModify)
Unfortunately, this gives the error Object doesn't support this property or method, any help will be appreciated.