遍历文件时未设置对象变量或 With Block 变量

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

我正在尝试 运行 一个做三件事的宏:

  1. 循环遍历一系列 excel 个文件
  2. 标识包含文本“项目属性”的行
  3. 使用此行设置范围以执行合并操作

我用在其他地方找到的代码构建块构建了这个,我知道每个代码都是独立工作的(即我可以 运行 遍历所有文件而不执行任何操作,并且我可以识别行并执行merge) 但是当我组合它们时,我得到一个 运行-time 91 错误“未设置对象变量或块变量”- 与此行相关联“FindRowNumber = FindRow.Row”。

寻找有关如何避免将此变量设置为“无”的指导,因为它出现在 Watches window。

谢谢!

'''     
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

如评论中所述 - 您需要考虑 Find 未匹配的原因:

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