search工作簿A中的项目,如果find,则将行复制到工作簿B.

我需要根据searchstring从工作簿A复制到B. search部分似乎是从debugging确定,但复制不起作用。 有什么我错了吗?

Set wbThis = ActiveWorkbook Set wsNewData = wbThis.Sheets("Sheet1") lNextRow = 1 Set wbData = Application.Workbooks.Open(FileName, ReadOnly:=True) ThisWorkbook.Activate For Each ws In wbData.Worksheets With ws For Each Cell In ws.Range("H:H") If Cell.Value = fWhat Then matchRow = Cell.Row 'ws.Rows("8:" & matchRow).Select 'Selection.Copy ws.Rows(matchRow, "8").Copy wsNewData.Rows(lNextRow) wsNewData.Select wsNewData.Rows(lNextRow).Select wsNewData.Paste lNextRow = lNextRow + 1 wbThis.Save End If Next End With Next wbData.Close 

你的代码在某些地方是多余的。 虽然我认为最大的问题是贯穿整个色彩H的每个单元,这需要很长时间。 这里是代码,清理:

 Set wbThis = ActiveWorkbook Set wsNewData = wbThis.Sheets("Sheet1") lNextRow = 1 Set wbData = Application.Workbooks.Open(FileName, ReadOnly:=True) ThisWorkbook.Activate For Each ws In wbData.Worksheets For Each Cell In intersect(ws.Range("H:H"),ws.usedrange) If Cell.Value = fWhat Then ws.Rows(Cell.Row).Copy wsNewData.Rows(lNextRow) lNextRow = lNextRow + 1 End If Next Next wbThis.Save wbData.Close 'you are closing this withouth saving. are you sure you want to do this???? just delete this line... 

另一个问题是,显然你是真正的VBA的初学者,并在一般的编程。 为什么不从macroslogging器开始,并分析它logging的代码? 另外,在面向对象的编程和VBA上也要稍微阅读一下。 我很抱歉,但我无法解释我所做的一切,我想我必须从亚当和夏娃开始。

希望这个作品。

另外,下一次,只要做一个自动filter,并用macroslogging器logging。 甚至会比这更快。

看起来就像你到达那里,但是你有一些方法被弄清楚,副本似乎不清楚的来源和destinaton。

 Dim wbThis As Workbook, wbData As Workbook Dim ws As Worksheet, wsNewData As Worksheet Dim cell As Range Dim lNextRow As Long, matchRow As Long Dim fWhat As String, fileName As String fWhat = "thing to find" fileName = Environ("TEMP") & Chr(92) & "myWorkBook.xlsb" Set wbThis = ActiveWorkbook Set wsNewData = wbThis.Sheets("Sheet1") Set wbData = Application.Workbooks.Open(fileName, ReadOnly:=True) lNextRow = 1 For Each ws In wbData.Worksheets With ws For Each cell In Intersect(.UsedRange, .Range("H:H")) If cell.Value = fWhat Then matchRow = cell.Row .Rows(matchRow).Copy wsNewData.Rows(lNextRow).Cells(1) lNextRow = lNextRow + 1 wbThis.Save End If Next cell End With Next ws wbData.Close SaveChanges:=False Set wbThis = Nothing Set wsNewData = Nothing Set wbData = Nothing 

我已经使用工作表中正在检查的全行副本到wsNewData工作表上的下一行(wbThis的Sheet1)。

当您处于With … End With语句中时 ,您不必继续引用With … End With引用。 只要在range / .Rows等前面加上句点,他们就会知道父工作表是唯一被With … End With引用的。

我也必须发明一个文件名和f寻找。 你将需要自己设置软pipe。