2017-04-11 76 views
-4

我现在尝试此代码,并在目标中给我一个错误。我不知道我做错了什么。我尝试了几次,现在是一个完全不同的东西。从工作表中找到一个值并将工作表1中的行复制到工作表2

子Find_First()

Dim FindString As String 
    Dim Rng As Range 
    Dim RowCnt As Long 

    FindString = Sheets("sheet1").Range("F5").Value 

    If Trim(FindString) <> "" Then 
     With Sheets("sheet2").Range("A:A") 
      Set Rng = .Find(What:=FindString, _ 
          After:=.Cells(.Cells.Count), _ 
          LookIn:=xlValues, _ 
          LookAt:=xlWhole, _ 
          SearchOrder:=xlByRows, _ 
          SearchDirection:=xlNext, _ 
          MatchCase:=False) 
      If Not Rng Is Nothing Then 

      RowCnt = Rng.Row 

      Worksheets("sheet1").Range("p13:af13").Copy Destination:=Worksheets("sheet2").Range(Cells(RowCnt, 1)) 




       Application.Goto Rng, True 
      Else 
       MsgBox "Nothing found" 
      End If 
     End With 
    End If 
End Sub 
+0

我们如何帮助您? – Variatus

+0

我需要一个宏,它将搜索sheet1中的单元格f5中的值,并在sheet2中的列A中进行搜索。如果找到该值。 o想要将sheet1(P13:AF13)中的单元格范围复制到表单2中找到该值的行中 –

+0

您尝试过什么?你的代码的哪部分不能按照你的预期工作? – YowE3K

回答

0

比方说,你要搜索在Sheet1的A列中的字符“X”,如果找到,复制,行粘贴到Sheet2,然后运行下面的脚本。

Sub LastRowInOneColumn() 
    Dim LastRow As Long 
    Dim i As Long, j As Long 

    'Find the last used row in a Column: column A in this example 
    With Worksheets("Sheet1") 
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    End With 

    'MsgBox (LastRow) 
    'first row number where you need to paste values in Sheet1' 
    With Worksheets("Sheet2") 
     j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 
    End With 

    For i = 1 To LastRow 
     With Worksheets("Sheet1") 
      If .Cells(i, 1).Value = "X" Then 
       .Rows(i).Copy Destination:=Worksheets("Sheet2").Range("A" & j) 
       j = j + 1 
      End If 
     End With 
    Next i 
End Sub 

这就是你想要的吗?

相关问题