Excel使用查找时停止工作

我正在寻找一个代码来find“1263_Estamp_En”在范围“J1”,并select相同的列并将其粘贴到另一个工作簿,但运行代码excel停止工作,并重新启动本身,请帮助如何可以我search范围并select值。 以下是代码…

Sub Search() Dim A As Range Dim myRng As Range Dim R As Range Dim Col ThisWorkbook.Sheets("Result").Activate Set R = Range("A1:Z1") ThisWorkbook.Sheets("Sheet1").Activate Set A = Range("A1") myRng = R.Find(what:=Str(A), LookIn:=xlValue) Cells(myRng.Row, myRng.Column).Select Col = Selection.Column Col.select Range(selection,selection.end(xldown)).copy Thisworkbook.Sheets("Sheet2").Activate Range("A1").Pastespecial xlPasteValues End Sub 

我想你正在寻找类似下面的代码(没有所有不必要的ActivateSelectionSelect ):

 Option Explicit Sub Search() Dim A As Range Dim myRng As Range Dim R As Range Dim Col Set R = Sheets("Result").Range("A1:Z1") Set A = Sheets("Sheet1").Range("A1") Set myRng = R.Find(what:=CStr(A.Value), LookIn:=xlValue, lookat:=xlWhole) If Not myRng Is Nothing Then ' <-- check if Find was successful ' rest of your code goes here myRng.EntireColumn.Copy <-- copy entire Column where found Sheets("Sheet2").Range("A1").PasteSpecial xlPasteValues End If End Sub