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!
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