Reputation: 1
I want to have a macro which makes the followings:
With this I can open and modify all files well, and copy the cells but only every time to A3 and B3, not under each other.
Thanks
Sub OpenAllWorkbooks()
Dim MyFiles As String
MyFiles = Dir("D:\GTMS\AKL Laser 4 W27_36\*.xls")
Do While MyFiles <> ""
Workbooks.Open "D:\GTMS\AKL Laser 4 W27_36\" & MyFiles
Range("I36").Value = 2.03
Range("I37").Value = 2.19
Range("I48").Copy _
Workbooks("AKL LASER SUM W27_36 macro1.xls").Worksheets("Munka1").Range("A3")
Range("L36").Copy _
Workbooks("AKL LASER SUM W27_36 macro1.xls").Worksheets("Munka1").Range("B3")
MsgBox ActiveWorkbook.Name
ActiveWorkbook.Close SaveChanges:=True
MyFiles = Dir
Loop
End Sub
Upvotes: 0
Views: 124
Reputation: 23974
If you add a variable to keep track of which row you are currently writing to, it becomes quite easy:
Sub OpenAllWorkbooks()
Dim MyFiles As String
Dim destRow As Long
destRow = 3
MyFiles = Dir("D:\GTMS\AKL Laser 4 W27_36\*.xls")
Do While MyFiles <> ""
Workbooks.Open "D:\GTMS\AKL Laser 4 W27_36\" & MyFiles
With ActiveWorkbook.Worksheets(1)
.Range("I36").Value = 2.03
.Range("I37").Value = 2.19
Workbooks("AKL LASER SUM W27_36 macro1.xls").Worksheets("Munka1").Cells(destRow, "A").Value = .Range("I48").Value
Workbooks("AKL LASER SUM W27_36 macro1.xls").Worksheets("Munka1").Cells(destRow, "B").Value = .Range("L36").Value
destRow = destRow + 1
End With
MsgBox ActiveWorkbook.Name
ActiveWorkbook.Close SaveChanges:=True
MyFiles = Dir
Loop
End Sub
I also changed the code slightly so that it doesn't use a Copy
command - that sometimes causes issues if the user is doing anything else which uses the clipboard while the macro is running.
Upvotes: 1