按列名复制多个列

时间:2013-08-07 22:58:10

标签: vba

我正在尝试将列复制到另一个按列名称运行的工作表。以下代码的问题在于它仅复制价格计算器状态列。它覆盖了另外两个。是否有更好的方法来修改此代码,使其附加而不是覆盖?

Dim aCell1,aCell2,aCell3 As Range     Dim strSearch As String

strSearch1 = "Change Request Description"
strSearch2 = "Current State"
strSearch3 = "Price Calculator Status"

'Set ws = ThisWorkbook.Sheets(1)

With wrkbk
    Set aCell1 = Sheets("3. PMO Internal View").Rows(1).Find(What:=strSearch1, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

   'Sheets("3. PMO Internal View").Columns(aCell.Column).Copy

    Set aCell2 = Sheets("3. PMO Internal View").Rows(1).Find(What:=strSearch2, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    'Sheets("3. PMO Internal View").Columns(aCell.Column).Copy

    Set aCell3 = Sheets("3. PMO Internal View").Rows(1).Find(What:=strSearch3, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    'If Not aCell Is Nothing Then
       ' MsgBox "Value Found in Cell " & aCell.Address & vbCrLf & _
       ' "and the column number is " & aCell.Column

        '~~> Do the copying here
        Sheets("3. PMO Internal View").Columns(aCell1.Column).Copy
        Sheets("3. PMO Internal View").Columns(aCell2.Column).Copy
        Sheets("3. PMO Internal View").Columns(aCell3.Column).Copy
    'Else
        'MsgBox "Search value not found"
    'End If
End With

1 个答案:

答案 0 :(得分:0)

将您的副本行更改为:

Sheets("3. PMO Internal View").Range(Sheets("3. PMO Internal View").Columns(aCell1.Column).Address & "," & Sheets("3. PMO Internal View").Columns(aCell2.Column).Address & "," & Sheets("3. PMO Internal View").Columns(aCell3.Column).Address).Copy

这会在一个步骤中选择您的列,如范围(“A:A,C:C,E:E”)等多个区域。逗号是文本字符串添加,就像在range命令中使用逗号一样,它具有不同的含义。