我正在编写一个代码来查找在“J1”范围内有“1263_Estamp_En”的行,并选择同一列并将其粘贴到另一个工作簿但是在运行代码excel时停止工作并重新启动,请帮助我如何搜索范围并选择值。以下是代码......
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
答案 0 :(得分:2)
我认为您正在寻找类似下面代码的内容(没有不必要的Activate
,Selection
和Select
):
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