由于macros的Excel应用程序崩溃

在启动我的macros期间,Excel应用程序崩溃。 如果我用一个整数testingmacros,程序运行正常(partnumber = 123)。 如果我检查一个string应用程序崩溃。 因此,没有错误代码对我来说是可见的。 我认为有一个types不匹配(但我设置零件号变式)

Sub SbIsInCOPexport() Dim lastRow As Long Dim i As Long Dim found As Boolean Dim partnumber As Variant i = 1 found = False partnumber = ActiveCell.Value Windows("COPexport.xlsx").Activate lastRow = Sheets(1).Cells.SpecialCells(xlLastCell).Row Do While i < lastRow + 1 If Cells(i, 6).Value = partnumber Then found = True Exit Do End If i = i + 1 Loop If found = True Then Cells(i, 6).Select MsgBox ("Searched part number: " & Str(partnumber) & vbNewLine & "Found part number: " _ & ActiveCell.Value & vbNewLine & "Address: " & Cells(i, 6).Address & vbNewLine & vbNewLine & "Test Order: " & _ Cells(i, 2).Value) Windows("COPexport.xlsx").Activate Else MsgBox "Part number is not found in the COP samples!" Windows("COPexport.xlsx").Activate End If End Sub 

什么是根本原因?

我没有看到任何明显的问题,但考虑使用范围对象的.Find方法,如下所示:

 Sub SbIsInCOPexport() Dim partnumber as Variant Dim rng as Range Windows("COPexport.xlsx").Activate partnumber = ActiveCell.Value Set rng = Columns(6).Find(partnumber) '## Search in column 6 for partnumber If rng Is Nothing Then MsgBox "Part number is not found in the COP samples!" Windows("COPexport.xlsx").Activate Else With rng MsgBox ("Searched part number: " & Str(partnumber) & vbNewLine & _ "Found part number: " & .Value & vbNewLine & _ "Address: " & .Address & vbNewLine & vbNewLine & _ "Test Order: " & .Offset(0,-4).Value) '## Get the value from column 2 End With End If End Sub