用于在大型数据库中搜索装配编号的宏,将结果粘贴到另一个工作表中

时间:2017-09-29 13:23:14

标签: excel vba excel-vba

我是VBA编码的新手,但我基本上想要为这个YouTube视频创建一个类似的功能: https://www.youtube.com/watch?v=QOxhRSCfHaw

我有两张兴趣,Stencils和Search。我希望能够在“搜索”选项卡中键入程序集编号,按下宏按钮,让它搜索“模板”选项卡并粘贴它找到的与程序集编号匹配的数据。可能我可能需要改变我定义变量的方式,不确定。

我可以毫无错误地运行代码。代码清除目标单元格的内容。但是,它不粘贴任何东西。

Sub findstencil()

'1. declare variables
'2. clear old search results
'3. find records that match search criteria and paste them


Dim assembly As String 'Assembly number of interest, containts numbers, letters and dashes
Dim finalrow As Integer 'determines last row in database
Dim i As Integer 'row counter


'clears destination cells
Sheets("Search").Range("A7:H15").ClearContents



assembly = Sheets("Search").Range("A5").Value
finalrow = Sheets("Stencils").Range("C5000").End(xlUp).Row

For i = 5 To finalrow
    If Cells(i, 8) = assembly Then
        Sheets("Stencils").Range(Cells(i, 2), Cells(i, 8)).Copy
        Sheets("Search").Range("B15").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
        End If

Next i

Sheets("Search").Range("A5").Select


End Sub

0 个答案:

没有答案