简体   繁体   English

根据单元格值将行复制到工作簿表中

[英]Copy rows into a Workbook sheet based on cell value

Am very new and have been trying my best to self help myself but after a hour or so I have give up and now need help, I am trying to use the below code I have found on here to copy the "OK" lines in Workbook 1 sheet 1 column I into a new sheet in workbook 2 sheet 1. 我是一个非常新的人,一直在尽我所能来自助,但是大约一个小时后,我放弃了,现在需要帮助,我正在尝试使用下面的代码在我的工作簿中复制“ OK”行从工作表2工作表1中的新工作表中将1页1列I

I don't need to copy the "ERROR" part but have left this in until I got the code working. 我不需要复制“错误”部分,但是在我让代码正常工作之前一直保留此部分。

I would also like to keep all the data copied in to workbook 2 as at the moment the previous date is being replaced with the new data. 我还想将所有数据复制到工作簿2中,因为之前的日期已被新数据替换。

    FilterAndCopy()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual


Dim lngLastRow As Long
Dim OKSheet As Worksheet, ErrorSheet As Worksheet

Set OKSheet = Sheets("Sheet1") ' Set This to the Sheet name you want all Ok's going to
Set ErrorSheet = Sheets("Sheet2") ' Set this to the Sheet name you want all Error's going to

lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row


With Range("A1", "I" & lngLastRow)
    .AutoFilter
    .AutoFilter Field:=9, Criteria1:="OK"
    .Copy OKSheet.Range("A1")
    .AutoFilter Field:=9, Criteria1:="ERROR"
    .Copy ErrorSheet.Range("A1")
    .AutoFilter
End With


Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub

Thanks 谢谢

Well your code is not working because you need to write 好吧,您的代码无法正常工作,因为您需要编写

 sub FilterAndCopy()

At the beggining of your sub, try it and see if it works. 在您的潜艇开始时,尝试一下,看看它是否有效。

What you can do to check if there is data in the colum is: 您可以检查列中是否有数据:

Dim r as range
set r = Range("a1")
 while r.value <> ""
  r= r.offset(0,1)
 end while

and use "r" as your paste range instead of A1 并使用“ r”代替A1作为粘贴范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据其他工作簿中其他工作表中的单元格值(月份名称)选择要复制到的工作表 - Selecting sheet to copy to based on cell value(Month name) from other sheet in other workbook Excel VBA 将工作表复制到新工作簿并重命名基于工作表的单元格值 - Excel VBA Copy sheet to new workbook with rename sheet based cell value 根据对一个单元格值的搜索将新行复制到 Excel 工作表中 - Copy new rows into excel sheet based on search on one cell value 根据单元格值将行复制到工作表底部并按升序排序 - Copy rows to bottom of sheet based on cell value and sorted ascending wise 根据单元格值查找所有行,然后复制到其他工作表。 - Find all rows based on a cell value and copy to different sheet. 根据单元格值将多张纸中的行复制到一张纸中 - Copy rows from multiple sheets into one sheet based on a cell value 根据单元格值复制行并粘贴到新工作表上 - Copy rows based on cell value and paste on a new sheet 根据工作表1中的单元格值隐藏工作表1和2中的行 - Hide rows in sheet 1 and 2 based on cell value in sheet 1 根据单元格自动将行复制到工作表 - Automatically copy rows to sheet based on cell 根据单元格值复制行并粘贴到具有相同单元格值名称的新工作表上 - Copy rows based on cell value and paste on a new sheet with same cell value name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM