简体   繁体   English

Excel VBA过滤一列,然后将区域集合中的每个项目复制到匹配的行

[英]Excel VBA filtering a column and then copying each item in the area collection to a matching row

I have the following Excel Table: 我有以下Excel表:

 Create Date          Last Active Date        Age
 4/12/2017 5:54         4/17/2020 8:54      5 Days
                        4/19/2017 7:43       #N/A
                        4/12/2017 20:43      #N/A
 4/1/2017 23:20         4/3/2017 6:54       10 Days
                        4/15/2017 22:20      #N/A

What I want to do is to filter the Age Column by #N/A, and then copy each Last Active Date value to the same row in Create Date. 我要执行的操作是按#N / A过滤“年龄”列,然后将每个“上次活动日期”值复制到“创建日期”中的同一行。 Seems easy enough, but I keep running into issues. 似乎很容易,但是我一直遇到问题。 I am using the SpecialCells(xlCellTypeVisible) property to then do a for each on each Area in there(non-contiguous rows), but when I go to copy the rows, it either copies the rows starting at Row 1 of the Create Date column, meaning the values get all out of whack OR it throws an error saying the ranges don't match. 我正在使用SpecialCells(xlCellTypeVisible)属性对其中的每个Area(非连续行)执行每个操作,但是当我复制行时,它要么复制从Create Date列的第1行开始的行,表示这些值全都用尽了,否则将引发错误,指出范围不匹配。 Here is the code I have so far that I pulled from another page that talked about how to do this, but it doesn't seem to work for me. 这是我到目前为止的代码,是我从另一页上讨论了如何执行此操作的,但是对于我来说似乎无效。

    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8, Criteria1:= _
        "#N/A"
    Dim lngrow As Long
    Dim FinalDest As Range
    Dim Rng As Range
    lngrow = Sheets("Sheet1").UsedRange.Rows.Count
    Set FinalDest = Sheets("Sheet1").Range("C2:C" & lngrow)
    Range("F2:F" & lngrow).Select

    For Each Rng In Cells.SpecialCells(xlCellTypeVisible).Areas
        Set FinalDest = FinalDest.Offset(Rng.Rows.Count)
        Rng.Copy Destination:=FinalDest
    Next Rng
    Application.CutCopyMode = False

How can I accomplish this? 我该怎么做? I want to filter it by #N/A, and then for each filtered row that remains, copy the value in Last Active Date to Create Date, (which will always be blank for these rows) and make sure they get copied to the proper rows, ie if Row 3 is the first filtered row, the value gets copied to rows 3 instead of row 2. 我想按#N / A进行过滤,然后对于剩余的每个过滤行,将“上次活动日期”中的值复制到“创建日期”(对于这些行将始终为空白),并确保将它们复制到正确的位置行,即如果第3行是第一个过滤的行,则将值复制到第3行而不是第2行。

You don't need to filter your table. 您无需筛选表格。 You have two feasible solutions: 您有两个可行的解决方案:

Solution 1 (without VBA): Assuming that Create Date is at A1: 解决方案1(无VBA):假定“创建日期”为A1:

-Insert a new column between Create Date and Last Active Date. -在“创建日期”和“上次活动日期”之间插入新列。

-Insert this function to B2--> =IF(A2="",IFNA(D2,C2),A2) -将此函数插入​​B2-> = IF(A2 =“”,IFNA(D2,C2),A2)

-Copy paste B2 till end of your column -将粘贴B2复制到您的列的末尾

-Copy all B column and paste them as values. -复制所有B列并将其粘贴为值。

-Delete Column A -删除列A

Solution 2 (with VBA): 解决方案2(使用VBA):

You can loop through each row and check if the Age cell is #N/A, if true(#N/A), then you do the copy/paste from Last Active Date cell to Create Date Cell. 您可以遍历每一行并检查“年龄”单元格是否为#N / A,如果为true(#N / A),则可以从“上次活动日期”单元格复制/粘贴到“创建日期”单元格。

Solution 1 is much faster and easy. 解决方案1更快,更简单。 If you are interested in VBA solution let us know. 如果您对VBA解决方案感兴趣,请告诉我们。

Edit: You can always change the following code for different conditions but since you said --> Create date value (which will always be blank for these rows): 编辑:您始终可以针对不同的条件更改以下代码,但由于您说的是->创建日期值(这些行始终为空白):

Dim i As Long
For i = 2 To Range("C1").End(xlDown).Row
    If IsEmpty(Cells(i, 1).Value) Then
        Cells(i, 1) = Cells(i, 2)
    End If
Next i

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM