Hi, I would be incredibly grateful if anyone could help me with a problem I am having with a macro on excel 2010. I'm in my final year of my PhD and this is really causing me a lot of delays that I fear could have a detrimental effect on being able to submit my thesis on time. Data processing could take months instead of weeks at this rate - scary! Any suggestions would be very gratefully received (sorry this is such a long message).
Regards, Donna
Brief background
I've been using this macro for many months. The macro compiles a single column of data from upto 1104 .csv files into a single .xls file. For months it has worked perfectly, taking no more than 15 minutes.
Recent trouble
In past couple of weeks it has slowed considerably for no apparent reason. It does still sometimes eventually finish running as previous albeit taking longer. However most of the time it seems to take forever and when I press Esc this error always appears - Run-time error '1004': Method 'Open' of object 'Workbooks' failed. And when selecting Debug the same line of code is always highlighted yellow. On hitting continue it will run for a few more .csv files and the same occurs again and AGAIN!
And here is the code (I've highlighted the line of code it stops on)
Regards, Donna
Brief background
I've been using this macro for many months. The macro compiles a single column of data from upto 1104 .csv files into a single .xls file. For months it has worked perfectly, taking no more than 15 minutes.
Recent trouble
In past couple of weeks it has slowed considerably for no apparent reason. It does still sometimes eventually finish running as previous albeit taking longer. However most of the time it seems to take forever and when I press Esc this error always appears - Run-time error '1004': Method 'Open' of object 'Workbooks' failed. And when selecting Debug the same line of code is always highlighted yellow. On hitting continue it will run for a few more .csv files and the same occurs again and AGAIN!
And here is the code (I've highlighted the line of code it stops on)
Code:
Sub compiledata_U_velocity()
Dim MyFile As String
Dim MyCurrFile As String
Dim myformula
Dim ColumnTitle As String
Dim MyLocation As String
MyCurrFile = ThisWorkbook.Name
'You will need to change the file path to match the
'location of where your csv files are stored
MyLocation = "G:\Single phase trials 3.30\Export - hd90 Single Phase Trials\deltaT100\32 50%\-1rd U\"
MyFile = Dir(MyLocation & "*.csv")
ColumnTitle = MyFile
'This part of the code will open the first csv file in the folder
'and copy two columns, the distance and the v value and paste it
'into the Compiled document
Application.ScreenUpdating = False
Workbooks.Open MyLocation & MyFile
Range("F9:G105").Select
Selection.Copy
Workbooks(MyCurrFile).Worksheets(1).Activate
Range("a1").Select
ActiveSheet.Paste
Range("b1").Select
Range("b1") = ColumnTitle
With Selection
.WrapText = True
.ColumnWidth = 16.71
End With
'this section will convert the values to the
'absolute value i.e. the value without the sign
Range("b2").Select
myformula = ActiveCell.Value * -1
ActiveCell = myformula
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell = ""
myformula = ActiveCell.Value * -1
ActiveCell = myformula
ActiveCell.Offset(1, 0).Select
Loop
Workbooks(MyFile).Close savechanges:=False
MyFile = Dir
ColumnTitle = MyFile
'this section will look for the next csv file in the folder
'and perform the same actions
Do Until MyFile = ""
[B][COLOR="#FF0000"]Workbooks.Open MyLocation & MyFile[/COLOR][/B]
Range("G9:G105").Select
Selection.Copy
Workbooks(MyCurrFile).Worksheets(1).Activate
Range("a1").Select
ActiveCell.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
ActiveCell = ColumnTitle
With Selection
.WrapText = True
.ColumnWidth = 16.71
End With
ActiveCell.Offset(1, 0).Select
myformula = ActiveCell.Value * -1
ActiveCell = myformula
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell = ""
myformula = ActiveCell.Value * -1
ActiveCell = myformula
ActiveCell.Offset(1, 0).Select
Loop
Workbooks(MyFile).Close savechanges:=False
MyFile = Dir
ColumnTitle = MyFile
Loop
End Sub
Last edited by a moderator: