问题描述
我有 2 个环境,一个是 MS Office 365 企业应用,另一个是 professional plus 2016。 宏代码在企业版的 MS Office 365 APP 中运行良好,但在 Professional plus 2016 中的 Selection.Find 功能中面临“下标超出范围”异常。
在调试时注意到 xlFormulas2 为空,这可能会导致错误,但不确定为什么在 Professional plus 2016 中 xlFormulas2 为空,但在其他环境中为 -4185 可以正常工作。
Sub AA_01_Spring_China_Individual()
sNumber = InputBox("Which week you would like to update?","Week No:")
sResponse = "Week " & sNumber
Sheets("BD Calls").Select
Columns("A:A").Select
Selection.Find(What:="Beijing",After:=ActiveCell,LookIn:=xlFormulas2,_
LookAt:=xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,_
MatchCase:=False,SearchFormat:=False).Activate
解决方法
可能是 xlFormulas2
适用于 Office 365。要使查找在两种情况下都能正常工作,您可以避免类似 ...
Sub Macro1()
Dim foundCl As Range
On Error Resume Next
Set foundCl = Selection.Find(What:="Beijing",After:=ActiveCell,LookIn:=xlFormulas2,LookAt:= _
xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False _,SearchFormat:=False)
If foundCl Is Nothing Then
Set foundCl = Selection.Find(What:="Beijing",LookIn:=xlFormulas,_
LookAt:=xlPart,_
MatchCase:=False,SearchFormat:=False)
End If
On Error GoTo 0
Debug.Print foundCl.Address
foundCl.Activate
End Sub