Object'_Global'的方法'Union'失败

我继续在第二次导出Excel电子表格时遇到这个错误。

我所看到的经常性答案是,联盟中使用的rng需要设置为无。 我一遍又一遍地遍历我的代码无济于事,还有什么我可能会失踪。

希望对此有所了解。

Set myRange = ApXL.Sheets(xlWSh.Name).Range("1:1") Set LastCell = myRange.Cells(myRange.Cells.count) Set FoundCell = myRange.Find(what:=fnd, After:=LastCell) If Not FoundCell Is Nothing Then FirstFound = FoundCell.Address Else GoTo NoValuesMatchingFound End If Set rng = FoundCell 'Loop until cycled through all unique finds Do Until FoundCell Is Nothing 'Find next cell with fnd value Set FoundCell = myRange.FindNext(After:=FoundCell) 'Add found cell to rng range variable Set rng = Union(rng, FoundCell) 'Select Cells Containing Find Value 'Test to see if cycled through to first found cell If FoundCell.Address = FirstFound Then Exit Do Loop rng.EntireColumn.Select ApXL.Selection.NumberFormat = "dd-mm-yy hh:mm:ss" 'Error Handler NoValuesMatchingFound: ' Debug.Print "No values were found in this worksheet" ' selects all of the cells ApXL.ActiveSheet.Cells.Select ' does the "autofit" for all columns ApXL.ActiveSheet.Cells.EntireColumn.AutoFit ' selects the first cell to unselect all cells xlWSh.Range("A1").Select On Error Resume Next xlWBk.Sheets("Sheet2").Delete xlWBk.Sheets("Sheet3").Delete On Error GoTo 0 With xlWBk If cmbOverwrite <> "Prompt" Then ApXL.DisplayAlerts = False Else ApXL.DisplayAlerts = True End If .SaveAs FileName:=txtSaveToFolder & "\" & File_Name & ".xlsx" Set rng = Nothing .Close End With rstXX.Close 

更改

 Set rng = Union(rng, FoundCell) 

成为

 Set rng = ApXL.Union(rng, FoundCell) 

因为您正在尝试使用属于Excel应用程序对象一部分的Union命令。