将EntireRow更改为仅复制选定的列

时间:2014-10-21 14:23:49

标签: vba excel-vba excel

我目前有这段代码

Sub TFS_Data_TC_Custody_Failed()
Dim bottomL As Integer
Dim x As Integer
bottomL = Sheets("TFS_Data_TC_Custody").Range("E" & Rows.Count).End(xlUp).Row: x = 3
Dim c As Range
For Each c In Sheets("TFS_Data_TC_Custody").Range("E2:E" & bottomL)
    If c.Value = "Failed" Then
        c.EntireRow.Copy Worksheets("TC_Custody_Failed").Range("A" & x)
        x = x + 1
    End If
Next c End Sub

我想将EntireRow更改为仅复制选定的列,例如它已识别的行的A,C,D。

2 个答案:

答案 0 :(得分:0)

就个人而言,我会完全不同地构建我的代码,但这是基于你拥有的一种方式。我并不完全了解您放置复制数据的位置,因此您可能需要修改该部分。

Dim bottomL As Integer
Dim x As Integer
bottomL = Sheets("TFS_Data_TC_Custody").Range("E" & Rows.Count).End(xlUp).Row
x = 3
Dim c As Range
For Each c In Sheets("TFS_Data_TC_Custody").Range("E2:E" & bottomL)
    If c.Value = "Failed" Then
        c.Cells(1, 1).Copy Worksheets("TC_Custody_Failed").Range("A" & x) 
        c.Cells(1, 3).Copy Worksheets("TC_Custody_Failed").Range("A" & x + 1)
        c.Cells(1, 4).Copy Worksheets("TC_Custody_Failed").Range("A" & x + 2)
        x = x + 3
    End If
Next c

答案 1 :(得分:0)

一种可能性是创建另一个Range对象并使用Intersect方法仅保留EntireRow

的A,C和D列
Sub TFS_Data_TC_Custody_Failed()
Dim bottomL As Integer
Dim x As Integer
bottomL = Sheets("TFS_Data_TC_Custody").Range("E" & Rows.Count).End(xlUp).Row: x = 3
Dim c As Range
Dim copyRange As Range
For Each c In Sheets("TFS_Data_TC_Custody").Range("E2:E" & bottomL)
    If c.Value = "Failed" Then
        Set copyRange = Application.Intersect(c.EntireRow, _
                        Sheets("TFS_Data_TC_Custody").Range("A:A,C:D"))
        copyRange.Copy Worksheets("TC_Custody_Failed").Range("A" & x)
        x = x + 1
    End If
Next c
End Sub