选择范围并复制

时间:2019-03-14 20:21:53

标签: excel vba

我正在尝试将数据从一个文件复制到另一个文件。每当我下载源文件时,范围都会更改。 我创建了一个宏,可以从一个工作簿复制到另一个工作簿,但是不起作用。

Sub Copy_Method()

Dim lRow As Long
Dim lCol As Long

lRow = Workbooks("Active Dealers with State.xlsx").Worksheets("Active Dealers With State").Cells(Rows.Count, 1).End(xlUp).Row
lCol = Workbooks("Active Dealers with State.xlsx").Worksheets("Active Dealers With State").Cells(1, Columns.Count).End(xlToLeft).Column

Workbooks("Active Dealers with State.xlsx").Worksheets("Active Dealers With State").Range("A4", Cells(lRow, lCol).Select).Copy _
    Workbooks("Working Sheet.xlsx").Worksheets("Active Dealer with State").Range("A4")

End Sub

这是什么问题?我要将整个数据从“带状态的活动经销商”工作簿复制到“工作表”文件中。

2 个答案:

答案 0 :(得分:1)

更多的变量和更明确的范围/单元格引用应该可以改善以下情况:

Sub Copy_Method()

    Dim lRow As Long
    Dim lCol As Long
    Dim wsSource as worksheet, wsDest as worksheet

    Set wsSource = Workbooks("Active Dealers with State.xlsx").Worksheets("Active Dealers With State")
    Set wsDest = Workbooks("Working Sheet.xlsx").Worksheets("Active Dealer with State")


    lRow = wsSource.Cells(Rows.Count, 1).End(xlUp).Row
    lCol = wsSource.Cells(1, Columns.Count).End(xlToLeft).Column

    'note qualified Cells reference below...
    With wsSource
        .Range(.Range("A4"), .Cells(lRow, lCol)).Copy wsDest.Range("A4")
    End With

End Sub

答案 1 :(得分:0)

我认为您不应该使用复制/粘贴功能,因为这样效率很低。您可以将值设置为彼此相等

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#795548</color>
    <color name="colorPrimaryDark">#6B4A3E</color>
    <color name="colorAccent">#FF000000</color
</resources>