PsychSilvia
New Member
- Joined
- Sep 17, 2017
- Messages
- 1
I am new to the VBA language and coding. I have a folder with 51 Excel files. In each file there are specific variables between cell J4 and cell R4, included, that I want to tranfer into an inclusive excel workbook. The code that I am using is the following:
<code>Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
MyFile = Dir("C:\Users\Aaa\Desktop\Analysed Data")
Do While Len(MyFile) > 0
If MyFile = "zmaster.xlsm" Then
Exit Sub
End If
Workbooks.Open (MyFile)
Range("J4:R4").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1,0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow,1),Cells(erow,51))
MyFile = Dir
Loop
End Sub
</code> Mainly this code does what I want: it transfer the range of values of my 51 different files into a new workbook. The format however for some values derived from formulas in the original files is messed up. I have tried to change my paste line as following:
ActiveSheet.PasteSpecial xlPasteValuesFormat Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 51))
But it is not working and I get the following error: "Compile error:expected:end of statement"
Any ideas on how I can solve my problem? Thank you.
<code>Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
MyFile = Dir("C:\Users\Aaa\Desktop\Analysed Data")
Do While Len(MyFile) > 0
If MyFile = "zmaster.xlsm" Then
Exit Sub
End If
Workbooks.Open (MyFile)
Range("J4:R4").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1,0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow,1),Cells(erow,51))
MyFile = Dir
Loop
End Sub
</code> Mainly this code does what I want: it transfer the range of values of my 51 different files into a new workbook. The format however for some values derived from formulas in the original files is messed up. I have tried to change my paste line as following:
ActiveSheet.PasteSpecial xlPasteValuesFormat Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 51))
But it is not working and I get the following error: "Compile error:expected:end of statement"
Any ideas on how I can solve my problem? Thank you.