VBA Code Not Working Consistently - Run Time Error 91

shellp

Board Regular
Joined
Jul 7, 2010
Messages
199
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
The following code is to take data from the "raw" worksheet and copy to separate workbooks based on the initials of each coder. When the Excel document is open and the macro selected, I get a run-time error 91 "Object variable or With block variable not set" but when I run it a second time it works exactly as planned (so it runs correctly every other time it is selected). The error occurs on line 18: "set Rng = sht.range(sht.autofilter.range(columns(4).address)". Any assistance greatly appreciated.

VBA Code:
Sub Coder_Own_Sheet()
    Dim Sht As Worksheet
    Dim Rng As Range
    Dim List As Collection
    Dim varValue As Variant
    Dim i As Long
    Dim filename As String
    Dim pathname
      
   Set Sht = ActiveWorkbook.Sheets("Raw")
   filename = "Cell Saver_" & Format(Sheets("General").Range("A1").Value, "MMM-yyyy") & "_" & varValue
   pathname = Sheets("General").Range("A2")
  
   With Sht.Range("A3")
   .AutoFilter
  End With
    
Set Rng = Sht.Range(Sht.AutoFilter.Range.Columns(4).Address)
Set List = New Collection

   On Error Resume Next
   For i = 2 To Rng.Rows.Count
    List.Add Rng.Cells(i, 1), CStr(Rng.Cells(i, 1))
   Next i
  For Each varValue In List
 Rng.AutoFilter Field:=4, Criteria1:=varValue

'       // Copy the AutoFiltered Range to new Workbook
    Sht.AutoFilter.Range.Copy
    Workbooks.Add
    Range("A1") = "Inpatient Case Review for Flagged Interventions - Cell Saver"
    Range("A2") = "Fix the error please"
    Range("A4").PasteSpecial xlPasteAll
    Range("A1").Font.Size = 16
    Range("A1").Font.Bold = True
    Columns("A").ColumnWidth = 14
    Columns("B:H").EntireColumn.AutoFit
    ActiveWorkbook.SaveAs filename:=pathname & filename & varValue & ".xlsx"
    ActiveWorkbook.Close savechanges:=True

'   // Loop back to get the next collection Value
   Next varValue

'   // Go back to main Sheet and removed filters
    Sht.AutoFilter.ShowAllData
    Sht.Activate
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
The only way to replicate your issue is to NOT declare the variable: Rng. Are you using Option Explicit ?
 
Upvote 0
Thanks for your response. No I wasn't using Option Explicit but adding it results the same...first attempt fails and second attempt works. Is there any other way to write this so it works on the first try?
 
Upvote 0
Change this part of your macro to this:
VBA Code:
With Sht
    .AutoFilterMode = False
    .Range("A3").AutoFilter
End With
 
Upvote 0
Solution
You are amazing, thank you so much, that totally fixed it!!
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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