简体   繁体   English

使用单元格值将数据从一张纸复制到另一张纸

[英]Copy data from one sheet to another with the cell value

I have a sheet "Data". 我有一个工作表“数据”。 With this sheet, I am looking into the column K. If it is red in colour then I am extract the complete row and copy them to another sheet " Delay". 在此工作表中,我正在查看K列。如果它是红色,则我提取完整的行并将其复制到另一张“ Delay”工作表中。

I am following the below code. 我正在下面的代码。 The code does not have any error but, It just copies 4 rows of red while I have 12 rows. 该代码没有任何错误,但是,当我有12行时,它仅复制4行红色。

Could anyone help me to find where I am wrong and what changes I need? 谁能帮助我找出我的错误之处以及我需要哪些改变?

Sub delay()
Dim cell As Range
Dim nextrow As Long
Dim a As Double

Application.ScreenUpdating = False
a = Application.WorksheetFunction.CountA(Sheets("Data").Range("K:K"))
For Each cell In Sheets("Data").Range("K5:K" & a)
If cell.DisplayFormat.Interior.Color = vbRed Then
nextrow = Application.WorksheetFunction.CountA(Sheets("Delayed").Range("K:K"))
Rows(cell.Row).Copy Destination:=Sheets("Delayed").Range("A" & nextrow + 1)
End If

Next
Application.ScreenUpdating = False
End Sub

First of all: 首先:
WorksheetFunction.CountA counts the number of cells that are not empty and the values within the list of arguments, you can't use it to count the total number of rows or to find number of the last row (unless all cells are not empty). WorksheetFunction.CountA计算不为空的单元格的数目以及参数列表中的值,您不能使用它来计算总行数或查找最后一行的数目(除非所有单元格都不为空) 。
What you might need is: 您可能需要的是:

nmbRows = Workbook("WorkbookName").Worksheet("SheetName").Range("K" & Rows.Count).End(xlUp).Row

or something less blunt. 或不那么钝的东西。
Using CountA can result in constriction of your range of search which will lead in data loss or, in your case, inserting a row in incorrect place. 使用CountA可能会导致搜索范围缩小,从而导致数据丢失,或者在您的情况下,将行插入错误的位置。
Example: 例:
Result of 的结果

Option Explicit
Sub test()
    Dim a As Long
    a = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("Ëèñò1").Range("A:A"))
    ThisWorkbook.Sheets("Ëèñò1").Range("B1").Value = a
End Sub

is
在此处输入图片说明
Second: 第二:
Be cautious, add a reference to each range , sheet as 请谨慎,为每个rangesheet添加参考

ThisWorkbook.Sheets("Data").Range("K5:K" & nmbRows)

By doing this you can always be certain you're refering to correct range you want to check. 这样一来,您就可以始终确定自己所指的是要检查的正确范围。

Another note: 另一个注意事项:
I'm not VBA expert but if I were you I would count number of rows for each sheet respectively at the start of the macro, in loop I would use nextrow=nextrow + 1 construction instead of calling function each time. 我不是VBA专家,但是如果您是我,我将在宏的开头分别计算每张纸的行数,在循环中,我将使用nextrow=nextrow + 1结构而不是每次调用函数。

暂无
暂无

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

相关问题 根据第一张工作表中第二个单元格的值,将数据从一个单元格复制到另一张工作表中的单元格? - Copy data from one cell to a cell in another sheet based on the value of a second cell in the first sheet? 根据单元格值将数据从一张表复制并粘贴到另一张表 - Copy and paste data from one sheet to another sheet(s) based on cell value 根据单元格中的值将行从一张表复制到另一张表中 - Copy rows from one sheet into another depending on value in cell 根据条件将单元格数据从一张纸复制到另一张纸 - copy cell data from one sheet to another based on condtion Excel 使用 VBA 将单元格数据从一张纸复制到另一张纸 - Excel Copy Cell Data from one sheet to another using VBA 根据单元格值将数据从特定列复制到另一个工作表 - Copy data from specific columns to another sheet based on a cell value 从一个单元格中查找值,从该单元格附近的单元格中复制值,然后将其粘贴到另一张纸上 - Find value from one cell, copy value from cell near to that cell and paste it to another sheet 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet 将单元格从一张纸复制到另一张纸 - Copy cell from one sheet to another 根据单元格值将数据从一张纸移动到另一张纸 - Moving Data from One Sheet to Another Based on Cell Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM