首页 > 解决方案 > VBA-复制没有特定列的行

问题描述

我有一个代码,它将根据缩写复制一行并将其发送到从某个单元格开始的另一个 Excel 工作表。我需要代码不复制缩写和行的其余部分。我该如何修复我的代码才能做到这一点。我需要排除的列是 A 列

桌子?数据 在此处输入图像描述

这是代码:

Private Sub CommandButton3_Click()

Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet

' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("Sheet1")
Set Target = ActiveWorkbook.Worksheets("Sheet2")

j = 11     ' Start copying to row # in target sheet
For Each c In Source.Range("A1:A1000")   ' range of the running log input
    If c = "PP" Then
     Source.Rows(c.Row).Copy Target.Rows(j) ' where i think problem is
       j = j + 1 ' makes it paste to the next open row

    End If
Next c

 j = 30
For Each c In Source.Range("A1:A1000")
    If c = "FA" Then
     Source.Rows(c.Row).Copy Target.Rows(j)
       j = j + 1

    End If
Next c
End Sub

标签: vbaexcel

解决方案


如果您知道要复制四个单元格,请尝试此操作。

Source.cells(c.Row,2).resize(,4).Copy Target.cells(j,1)

不知道为什么你有两个循环?自动过滤器会更有效,因为您可以完全取消循环。


推荐阅读