在excel中选择一列,但是从所需的位置

时间:2017-07-03 08:28:46

标签: excel vba excel-vba

我有两本工作簿,我想要的是将一列从一个Excel复制到另一个。 在列中,我想从标题下方选择数据直到底部 由于我的标题 USER 位于B14,所以我使用 .Find(What:=“User”)来查找标题并将其存储到aCell中。

但是当我将它复制到另一个excel时,它也选择B1到B13,这不是必需的。

我唯一想要的是从标题到底部(B14到B80)选择数据并将其粘贴到另一个excel中,如A1到A66。

Dim x As Workbook

Dim y As Workbook

Dim ws As Worksheet

Set x = Workbooks.Open("C:\file1.xls")

Dim aCell As Range


Set aCell = x.Sheets("file").Range("B1:B1000").Find(What:="User", LookIn:=xlValues,LookAt:=xlWhole, _
                                          MatchCase:=False, SearchFormat:=False)


Set y = Workbooks.Open("C:\file2.xls)

aCell.EntireColumn.Copy

y.Sheets("abc").Range("A1:A1000").PasteSpecial

End Sub

1 个答案:

答案 0 :(得分:2)

如果您不想复制EntireColumn,只需复制您感兴趣的范围:

Dim x As Workbook
Dim y As Workbook
Dim ws As Worksheet
Dim aCell As Range

Set x = Workbooks.Open("C:\file1.xls")
With x.Sheets("file")
    Set aCell = .Range("B1:B1000").Find(What:="User", _
                                        LookIn:=xlValues, _
                                        LookAt:=xlWhole, _
                                        MatchCase:=False, _
                                        SearchFormat:=False)

    'Check that header exists
    If aCell Is Nothing Then Exit Sub

    Set y = Workbooks.Open("C:\file2.xls")
    .Range(aCell, .Cells(.Rows.Count, aCell.Column).End(xlUp)).Copy _
                                                y.Sheets("abc").Range("A1")
End With