Excel 对象“\u Global”的方法“Union”失败
我在第二次导出excel电子表格时不断遇到此错误 我看到的反复出现的答案是,需要将联合中使用的rng设置为“无”。我在我的代码中一遍又一遍地这么做都没有用,还有什么我可能遗漏的吗 如果您对此有任何见解,我将不胜感激Excel 对象“\u Global”的方法“Union”失败,excel,vba,ms-access,Excel,Vba,Ms Access,我在第二次导出excel电子表格时不断遇到此错误 我看到的反复出现的答案是,需要将联合中使用的rng设置为“无”。我在我的代码中一遍又一遍地这么做都没有用,还有什么我可能遗漏的吗 如果您对此有任何见解,我将不胜感激 Set myRange = ApXL.Sheets(xlWSh.Name).Range("1:1") Set LastCell = myRange.Cells(myRange.Cells.count) Set FoundCell = myRange.Find(what:=fnd, A
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
改变
将来
因为您正在尝试使用Union命令,该命令是Excel应用程序对象的一部分。我看到的反复回答是,Union中使用的rng需要设置为nothing。-你在哪里读到的?设置foo=UnionRangeA1,则不会引发运行时错误5“过程调用或参数无效”。请注意,您需要检查foundCell是否为Nothing,并确保没有为Union提供任何未设置的引用。FWIW与错误无关-如果您的if foundCell.Address=FirstFound然后Exit Do在设置rng=Unionrng之前完成,则更符合逻辑,FoundCell或者你添加第一个找到的单元格两次都不重要-它会忽略第二个添加抱歉在最初发布时没有扩展,因为我时间很紧,然而,我试图理解的是,本论坛上发布的相同错误表明,特别是在第二次导出/运行时抛出工作簿rng之前未被清除。我想说的是,我已经尝试过这个解决方案了。例如,完全超出了我的想象,我的代码中还有其他对象,我也必须使用ApXL对象。谢谢你的提示。对于搜索类似问题的任何人,更改为设置rng=ApXL.Unionrng,FoundCell@CodeStack哎哟-我刚注意到我在键入答案时切换了你的对象名称-我在你使用ApXL现在编辑的答案时使用了xlAp-希望你还是注意到了
Set rng = Union(rng, FoundCell)
Set rng = ApXL.Union(rng, FoundCell)