Search code examples
excelvbaloopsfilesubroutine

Object variable or With Block variable not set when looping through files


I'm trying to run a macro that does three things:

  1. Loops through a series of excel files
  2. Identifies a row containing the text "project attributes"
  3. Uses this row to set a range to perform a merge operation

I constructed this out of building blocks of code I found elsewhere, and I know that each works independently (i.e. I can run through all files without performing actions, and I can identify the row and perform the merge) but when I combine them, I get a run-time 91 error "Object variable or With Block variable not set" - associated with this line " FindRowNumber = FindRow.Row ".

Looking for guidance as to how I can avoid having this variable set to "Nothing" as it appears in the Watches window.

Thanks!

'''     
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(Filename:=myPath & myFile)
    
    'Ensure Workbook has opened before moving on to next line of code
      DoEvents
    
    'Change First Worksheet's Background Fill Blue
        wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)
        Call RowStart(wb)
    
    'Save and Close Workbook
      wb.Close SaveChanges:=True
      
    'Ensure Workbook has closed before moving on to next line of code
      DoEvents

    'Get next file name
      myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

Sub RowStart(wb As Workbook)
Dim FindRowNumber As Long, FindRowStart As Integer, FindRow As Range

With wb.Worksheets("Project Details")
    Set FindRow = .Range("A:A").Find(What:="Project Attributes", LookIn:=xlValues, LookAt:=xlWhole)
    End With
    
    FindRowNumber = FindRow.Row
    FindRowStart = FindRowNumber + 1
    'MsgBox FindRowStart
    Call vba_merge(FindRowStart, wb)

End Sub

Solution

  • As noted in comments - you need to account for your Find not getting a match:

    Sub RowStart(wb As Workbook)
        Dim FindRow As Range
        
        With wb.Worksheets("Project Details")
            Set FindRow = .Range("A:A").Find(What:="Project Attributes", _
                                             LookIn:=xlValues, LookAt:=xlWhole)
        End With
        
        If Not FindRow Is Nothing Then
            vba_merge FindRow.Row + 1, wb  'use of `Call` is outdated....
        Else
            MsgBox "Row not found in workbook '" & wb.Name & "'", vbExclamation
        End If
    End Sub