我在Excel VSTO解决方案中遇到了.Findnext的一个奇怪问题。该代码在英语语言设置下运行良好,但在德语设置下失败并出现以下错误。
System.NullReferenceException:'对象引用未设置为对象的实例。'
调查时, EndRng = .FindNext(EndRng)什么也没有返回。这个块如何在英语环境下工作,但不能在德语下工作?
Dim StartRng As Excel.Range, EndRng As Excel.Range
Dim wrkSheet As Worksheet
Dim wb As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim xlapp As Excel.Application = Globals.ThisAddIn.Application
Dim FormulaToFind As String = FormulaToLocal(FormulaString)
wrkSheet.Range(wrkSheet.Cells(RowStart, ColStart), wrkSheet.Cells(RowEnd, ColEnd)).Select()
With xlapp.ActiveWindow.Selection
StartRng = .Find(FormulaToFind, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart)
If Not StartRng Is Nothing Then
EndRng = StartRng
StartAddress = StartRng.Address
Do
EndAddress = EndRng.Address
EndRng = .FindNext(EndRng)
Loop While Not EndRng Is Nothing And EndRng.Address <> StartAddress
End If
End With
以下是将公式转换为本地
的函数 Public Function FormulaToLocal(ByVal RefFormula As String) As String
Dim wb As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim TmpSheet As String = "TmpSheet"
wb.Worksheets(TmpSheet).Range("AZ1").Formula = RefFormula
FormulaToLocal= wb.Worksheets(TmpSheet).Range("AZ1").Formulalocal
wb.Worksheets(TmpSheet).Range("AZ1").value = ""
End Function
答案 0 :(得分:0)
Microsoft承认与此有关的一些问题。他们正在与高级工程师进行调查。以下是Microsoft社区的Andreas建议的替代解决方案。
With rngData
StartRng = .Find(FormulaToFind, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart,
SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlNext)
If Not StartRng Is Nothing Then
EndRng = StartRng
StartAddress = StartRng.Address
Do
EndAddress = EndRng.Address
EndRng = .Find(FormulaToFind, After:=EndRng, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart,
SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlNext)
Loop While Not EndRng Is Nothing And EndRng.Address <> StartAddress
End If
End With