简体   繁体   中英

Copy and paste after the last row VBA issue

I have a problem with my code. It does the trick of copy/paste correctly. However, I think there is something tricky. When I try to update my dynamic table it displays a message that the WB I'm currently working in already has data and if I want to replace it. When I choose "Yes/No" it immediately displays another column in my table that says that 81 registers are not been used in UTILITY . When I do everything by hand there are no problems. So, I guess there´s something wrong with my macro.

Option Explicit

Sub DailyTrans_MDM()

    Call CopyPaste
End Sub

Sub CopyPaste()

    Dim vFile As Variant
    Dim folderPath As String
    Dim wbCopyTo As Workbook
    Dim wsCopyTo As Worksheet
    Dim wbCopyFrom  As Workbook
    Dim wsCopyFrom As Worksheet

    vFile = Dir(folderPath & "*.xl*")
    Set wbCopyTo = ActiveWorkbook
    Set wsCopyTo = ActiveSheet
        Do While vFile <> ""        
            Application.ScreenUpdating = False    
            vFile = Application.GetOpenFilename("Daily Reports (*.xl*)," & "*.xl*", 1, "Select Report", "Open File", False)
            If TypeName(vFile) = "Boolean" Then
                Exit Sub
            Else
                Set wbCopyFrom = Workbooks.Open(vFile)
                Set wsCopyFrom = wbCopyFrom.Worksheets("ReporteCifrasControl")
            End If
    '--------------------------------------------------------------------------------------
            wsCopyFrom.Range("A2:M" & wsCopyFrom.Range("A" & Rows.Count).End(xlUp).row).Copy
            wsCopyTo.Range("A" & wsCopyTo.Range("A" & Rows.Count).End(xlUp).row + 1).PasteSpecial xlPasteValuesAndNumberFormats
            wbCopyFrom.Close SaveChanges:=False

            Dim rngCopy As Range, rngPaste As Range
                With ActiveSheet
                    Set rngCopy = .Range(.Range("A2"), Cells(2, Columns.Count).End(xlToLeft))
                    Set rngPaste = .Range(.Range("A2"), .Cells(Rows.Count, 1).End(xlUp)).Resize(, rngCopy.Columns.Count)
                End With
            rngCopy.Copy
            rngPaste.PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
        Loop

End Sub


I believe you want to do this

  • Copy row 2 from Column A to Last Used Column (LC)
  • Paste this in the first Non-Used Row (LR) in Column A

Dim LC As Long
Dim LR As Long

With ActiveSheet
    LC = .Cells(2, .Columns.Count).End(xlToLeft).Column
    LR = .Range("A" & .Rows.Count).End(xlUp).Row

    Set rngCopy = .Range(.Cells(1, 2), .Cells(LC, 2))
    Set rngPaste = .Range("A" & LR)
End With

rngCopy.Copy
rngPaste.PasteSpecial xlPasteFormats

You missed some objects to be qualified in your code. No point in using the With Block if you are not going to use the With Block


Just realized you have multiple copy/pastes in your code. If this is the wrong one, use the format here to modify the other one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM