VBA Error 1004 application-defined or object-defined error copy paste

macak333

New Member
Joined
Apr 12, 2021
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
Hi all!
Tried looking all over but can't figure out why the code isn’t working. I am trying to run a code that, when you open excel, starts running and c/p values from cca. 10 different excel in one folder (the one that you open). But I am always getting error 1004. Help is much appreciated!
VBA Code:
Private Sub Workbook_Open()

    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Dim LastRow As Long
    Dim strPath As String
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    ThisWorkbook.UpdateLinks = xlUpdateLinksNever
    Application.DisplayAlerts = False

    polna = 1
    taexcelzadnjavrstica = 0

    strPath = "C:\Users\Me\Desktop\teren"
    ChDir strPath
    strExtension = Dir("C:\Users\Me\Desktop\teren")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(Filename:=strPath & strExtension, UpdateLinks:=False)
        With wkbSource
           zadnjavrstica = wkbSource.Sheets("List1").Cells(Rows.Count, 1).End(xlUp).Row
            wkbSource.Sheets("List1").Range(Cells(2, 1), Cells(zadnjavrstica, 39)).Copy
            wkbDest.Sheets("List1").Range(Cells(polna + 1, 1), Cells(taexcelzadnjavrstica + zadnjavrstica, 39)).PasteSpecial xlValues 'this is the line that throws the error
            taexcelzadnjavrstica = taexcelzadnjavrstica + zadnjavrstica + 1
            polna = wkbDest.Sheets("List1").Cells(Rows.Count, 1).End(xlUp).Row
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
wkbDest.Sheets("List1").Range(Cells(2, 1), Cells(taexcelzadnjavrstica + zadnjavrstica, 39)).Sort Key1:=Range("a1"), Order1:=xlAscending

ThisWorkbook.UpdateLinks = xlUpdateLinksAlways
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,
welcome to forum

xlValues is not I think, a valid constant - replace it with xlPasteValues & see if resolves your issue.

Dave
 
Upvote 0
Hi,
welcome to forum

xlValues is not I think, a valid constant - replace it with xlPasteValues & see if resolves your issue.

Dave
Hi,
Thank you for the input but it still shows error.
The code runs when you open the excel and now it shows error 1004, method 'Cells' of object '_Global' failed.
 
Upvote 0
How about
VBA Code:
        With wkbSource.Sheets("List1")
           zadnjavrstica = .Cells(Rows.Count, 1).End(xlUp).Row
            .Range(.Cells(2, 1), .Cells(zadnjavrstica, 39)).Copy
            wkbDest.Sheets("List1").Range(wkbDest.Sheets("List1").Cells(polna + 1, 1), wkbDest.Sheets("List1").Cells(taexcelzadnjavrstica + zadnjavrstica, 39)).PasteSpecial xlPasteValues
            taexcelzadnjavrstica = taexcelzadnjavrstica + zadnjavrstica + 1
            polna = wkbDest.Sheets("List1").Cells(Rows.Count, 1).End(xlUp).Row
            .Parent.Close savechanges:=False
        End With
 
Upvote 0
How about
VBA Code:
        With wkbSource.Sheets("List1")
           zadnjavrstica = .Cells(Rows.Count, 1).End(xlUp).Row
            .Range(.Cells(2, 1), .Cells(zadnjavrstica, 39)).Copy
            wkbDest.Sheets("List1").Range(wkbDest.Sheets("List1").Cells(polna + 1, 1), wkbDest.Sheets("List1").Cells(taexcelzadnjavrstica + zadnjavrstica, 39)).PasteSpecial xlPasteValues
            taexcelzadnjavrstica = taexcelzadnjavrstica + zadnjavrstica + 1
            polna = wkbDest.Sheets("List1").Cells(Rows.Count, 1).End(xlUp).Row
            .Parent.Close savechanges:=False
        End With
Yes, yes, yes...now it works and doesn't throws erros.
Lots of thanks to both of you!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top