简体   繁体   English

对象“ _Global”的方法“联合”失败

[英]Method 'Union' of Object '_Global' failed

I keep obtaining this error on my second export of a excel spreadsheet. 我在第二次导出Excel电子表格时不断收到此错误。

The recurring answer I have seen is that the rng used in the union needs to be set to nothing. 我看到的重复回答是,工会中使用的rng需要设置为空。 I have done that over and over across my code to no avail, is there something else I might be missing. 我已经在我的代码中一遍又一遍地做到了这一点,但是没有任何帮助,我可能还缺少其他东西。

Would appreciate any insight on this. 希望对此有任何见识。

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

Change 更改

Set rng = Union(rng, FoundCell)

to be 成为

Set rng = ApXL.Union(rng, FoundCell)

because you are trying to use the Union command which is part of the Excel Application object. 因为您正在尝试使用作为Excel Application对象一部分的Union命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM