Copy from one workbook to another

2019-09-07 04:23发布

This should grab a range in the copyworkbooks then paste it to the master:

Sub activateMacro()
    Dim masterWKB, copyWKB As Workbook
    Set masterWKB = ThisWorkbook
    Dim masterWKS, copyWKS As Worksheet
    Set masterWKS = Worksheets("Data")
    Dim lastLineCopy, lastLineWKB As Long
    Dim pasteRange As Range
    Dim listofFileNames As Variant
    listofFileNames = buildfilenameArray

    for i = 0 to Ubound(listofFileNames)
        lastLineWKB = masterWKS.Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row + 1
        Debug.Print lastLineWKB
        Set copyWKB = Workbooks.Open("c:temp\" & listofFileNames(i, 0) & "")
        lastLineCopy = copyWKB.Sheets(1).Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
        Debug.Print lastLineCopy
        copyWKB.Sheets(1).Range("A15:AG" & lastLineCopy & "").Copy
        masterWKB.masterWKS.Range(masterWKS.Cells(lastLineWKB, 1)).PasteSpecial
    Next
end sub

I'm erroring out with object doesn't support his property or method at:

masterWKB.masterWKS.Range(masterWKS.Cells(lastLineWKB, 1)).PasteSpecial   

and I'm not sure why.

EDIT:

Got it to work with this but I wanted to avoid using activate.

masterWKB.Activate
masterWKS.Range("A" & lastLineWKB & "").PasteSpecial

2条回答
ゆ 、 Hurt°
2楼-- · 2019-09-07 04:32

Just change the line with the error to this:

masterWKB.masterWKS.Cells(lastLineWKB, 1).PasteSpecial
查看更多
放我归山
3楼-- · 2019-09-07 04:41

For some reason, when you use Cells inside of Range, you have to specify both the beginning and ending cell of the range (even if they're the same cell). A single cell reference will give the error you described. Try:

masterWKB.masterWKS.Range(masterWKS.Cells(lastLineWKB, 1),masterWKS.Cells(lastLineWKB, 1)).PasteSpecial

查看更多
登录 后发表回答