Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50076

Cannot copy data from one workbook to another

$
0
0
Hi all

I'm trying to copy data from one workbook to another, row by row, but I can't find the best way to copy the data and paste it across. This is my code:

Code:

Option Explicit

Sub ConfirmTransferOut()
    Dim wbk1 As Workbook
    Dim wbk2 As Workbook
    Dim CPHrow As Integer
    Dim livrow As Integer
    Dim apptype(1 To 40) As String
    Dim volume(1 To 40) As Integer
    Dim consignment(1 To 40) As Integer
    Set wbk1 = ActiveWorkbook
    Set wbk2 = Workbooks.Open("Liv L Received.xls")
    CPHrow = 3
    livrow = 4
    ' OPen Liv and find first empty row
    Do While Range("B" & livrow) <> Empty
        livrow = livrow + 1
    Loop
    Workbooks("CPH TrackerMONDAY.xlsx.xls").Activate
    ' Find first non-empty row where data not yet sent in CPH
    Do While Range("B" & CPHrow) <> Empty
        If Range("E" & CPHrow) = "Y" Then
            ' Copy the data from CPH to Liv
            wbk1.Range("B" & CPHrow & ":" & "D" & CPHrow).Select
            Application.CutCopyMode = False
            wbk1.Range("B" & CPHrow & ":" & "D" & CPHrow).Copy
            wbk2.Range("B" & livrow & ":" & "D" & livrow).Paste
            Application.CutCopyMode = False
            ' Go to next row in both workbooks
            CPHrow = CPHrow + 1
            livrow = livrow + 1
        End If
    Loop
    ActiveWorkbook.Close True
    wbk1.Activate
    Range("E" & CPHrow) = "Y"
End Sub

The problem I have is that is doesn't accept this command:

Code:

wbk1.Range("B" & CPHrow & ":" & "D" & CPHrow).Select
I'm also convinced that there's a much quicker way to copy and paste the data across without using four or five lines of code!

Viewing all articles
Browse latest Browse all 50076

Trending Articles