I am trying to create a macro that copies data from all the files in a directory to a file called costs.xls. But my macro keeps trying to open the same file. Your help is appreciated. Listed below is what I have.
Sub DirLoop()
Dim MyFile As String, Sep As String
' Sets up the variable "MyFile" to be each file in the directory
' This example looks for all the files that have an .xls extension.
' This can be changed to whatever extension is needed. Also, this
' macro searches the current directory. This can be changed to any
' directory.
' Test for Windows or Macintosh platform. Make the directory request.
Sep = Application.PathSeparator
If Sep = "\" Then
' Windows platform search syntax.
MyFile = Dir(CurDir() & Sep & "*.xls")
Else
' Macintosh platform search syntax.
MyFile = Dir("", MacID("XLS5"))
End If
' Starts the loop, which will continue until there are no more files
' found.
Do While MyFile <> ""
' Displays a message box with the name of the file. This can be
' changed to any procedure that would be needed to run on every
' file in the directory such as opening each file.
'MsgBox CurDir() & Sep & MyFile
'MyFile = Dir()
Workbooks.Open FileName:=MyFile
Sheets("03-SG&A").Select
Range("C2").Select
Selection.Copy
Range("A9:A163").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Columns("A:A").EntireColumn.AutoFit
Cells.Select
Application.CutCopyMode = False
'Filter Budget Lines
Selection.AutoFilter
Range("B5").Select
Selection.AutoFilter Field:=2, Criteria1:=">=50000", Operator:=xlAnd, _
Criteria2:="<80000"
Range("A9:O104").Select
Selection.Copy
Windows("costs.xls").Activate
Range("a1").Activate
Do While ActiveCell <> ""
ActiveCell.Offset(1, 0).Activate
Loop
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=False
Windows(MyFile).Activate
ActiveWorkbook.Close (False)
Loop
End Sub
Sub DirLoop()
Dim MyFile As String, Sep As String
' Sets up the variable "MyFile" to be each file in the directory
' This example looks for all the files that have an .xls extension.
' This can be changed to whatever extension is needed. Also, this
' macro searches the current directory. This can be changed to any
' directory.
' Test for Windows or Macintosh platform. Make the directory request.
Sep = Application.PathSeparator
If Sep = "\" Then
' Windows platform search syntax.
MyFile = Dir(CurDir() & Sep & "*.xls")
Else
' Macintosh platform search syntax.
MyFile = Dir("", MacID("XLS5"))
End If
' Starts the loop, which will continue until there are no more files
' found.
Do While MyFile <> ""
' Displays a message box with the name of the file. This can be
' changed to any procedure that would be needed to run on every
' file in the directory such as opening each file.
'MsgBox CurDir() & Sep & MyFile
'MyFile = Dir()
Workbooks.Open FileName:=MyFile
Sheets("03-SG&A").Select
Range("C2").Select
Selection.Copy
Range("A9:A163").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Columns("A:A").EntireColumn.AutoFit
Cells.Select
Application.CutCopyMode = False
'Filter Budget Lines
Selection.AutoFilter
Range("B5").Select
Selection.AutoFilter Field:=2, Criteria1:=">=50000", Operator:=xlAnd, _
Criteria2:="<80000"
Range("A9:O104").Select
Selection.Copy
Windows("costs.xls").Activate
Range("a1").Activate
Do While ActiveCell <> ""
ActiveCell.Offset(1, 0).Activate
Loop
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=False
Windows(MyFile).Activate
ActiveWorkbook.Close (False)
Loop
End Sub