title: Object Variable not Set error with autofilters
body: <p>I am creating the looping macro to fill missing dates between records (macro is still in progress, so do not pay attention to &quot;loop&quot; part). Unfortunately I am stopped by error in line with autofilters - sorting <code>Range(&quot;A:A&quot;)</code> - Object Variable or With block variable not set - runtime error 91.</p>
  <p>Do I need to declare variables also for sorting ranges?</p>
  <pre class="lang-vb prettyprint-override"><code>Sub Usage_report()    
      'OFF
      With Application
          .ScreenUpdating = False
          .DisplayAlerts = False
      End With
      
      Dim last_row As Long
      Dim latest_date As Variant
      Dim newest_date As Variant
      Dim i As Long
      Dim j As Long
      Dim days_between As Integer
      Dim All_range As Range
      Dim Range As Range
      Dim Range_firstlog As Range
      Dim Range_logYTD As Range
      
      last_row = ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Cells(Rows.Count, 1).End(xlUp).Row
      
      'converting A column to numbers
      Set All_range = ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(&quot;A2:A&quot; &amp; last_row)
      All_range.Select
      With Selection
          .NumberFormat = &quot;0&quot;
          .Value = .Value
      End With
      
      'sorting table by ID and date of logging (descending)
      ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).ShowAllData
      ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).AutoFilter.Sort.SortFields. _
              Clear
      ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).AutoFilter.Sort.SortFields. _
              Add2 Key:=Range(&quot;A:A&quot;), SortOn:=xlSortOnValues, Order:=xlDescending _
              , DataOption:=xlSortNormal
      ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).AutoFilter.Sort.SortFields. _
              Add2 Key:=Range(&quot;M:M&quot;), SortOn:=xlSortOnValues, Order:=xlDescending _
              , DataOption:=xlSortNormal
  
      With ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).AutoFilter.Sort
          .Header = xlYes
          .MatchCase = False
          .Orientation = xlTopToBottom
          .SortMethod = xlPinYin
          .Apply
      End With
            
      'finding the dates - newest and oldest
      latest_date = Format(WorksheetFunction.Min(ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(&quot;M1:M&quot; &amp; last_row)), &quot;mm/dd/yyyy&quot;)
      newest_date = Format(WorksheetFunction.Max(ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(&quot;M1:M&quot; &amp; last_row)), &quot;mm/dd/yyyy&quot;)
      
      'loop
      For i = last_row To 2 Step -1
          If (ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(&quot;H&quot; &amp; i).Value &lt; latest_date) And (Not IsEmpty(ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(&quot;M&quot; &amp; i).Value)) Then
              days_between = DateDiff(&quot;d&quot;, ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(&quot;M&quot; &amp; i).Value, newest_date)
              For j = 1 To days_between
                  Set Range = ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(Cells(i, 1), Cells(i, 13))
                  Range.Select
                  Selection.Copy
                  Selection.Insert Shift:=xlDown
                  Set Range_firstlog = ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(&quot;M&quot; &amp; i)
                  Set Range_logYTD = ActiveWorkbook.Worksheets(&quot;Usage report (logins)&quot;).Range(&quot;N&quot; &amp; i)
                  Range_firstlog.Offset(j, 0).Value = Range_firstlog.Value + 1
                  Range_logYTD.Offset(j, 0).Value = &quot;&quot;
              Next j
          End If
      Next i
      
      'ON
      With Application
          .ScreenUpdating = True
          .DisplayAlerts = True
      End With
  End Sub
  </code></pre>