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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,599
Office Version
  1. 2019
Platform
  1. Windows
Hi,
welcome to forum

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

Dave
 

macak333

New Member
Joined
Apr 12, 2021
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,413
Office Version
  1. 365
Platform
  1. Windows
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
 

macak333

New Member
Joined
Apr 12, 2021
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
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!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,413
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,133,268
Messages
5,657,745
Members
418,411
Latest member
Excellency

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
Top