title: Pass Worksheet to a function to autofit columns
body: <p>I have the following working code as part of a bigger function</p>
  <pre><code>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
  </code></pre>
  <p>Where strNewProjSheetName is a string.</p>
  <p>I'd like to extract the for loop in to a function. To do this I have</p>
  <pre><code>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
  </code></pre>
  <p>and call it with the following</p>
  <pre><code>Dim SheetToModify As Worksheet
  Set SheetToModify = Sheets(strNewProjSheetName)
  
  AutofitAllUsedColumns (SheetToModify)
  </code></pre>
  <p>Unfortunately, this gives the error Object doesn't support this property or method, any help will be appreciated.</p>