Hello....
I have found a VBA code in mrexcel from previous years before in regard to opening all files in a certain folder.
Below are the code and I modified it a bit.
The VBA below is supposed to go open a file in a certain folder. Copy it to another opened workbook, go to another sheet in that workbook and print it out.
Next go to the next file in that folder and repeat the task.
The thing is that I also need to put the file name that I just opened in cell W2 so that I can copy it together to the workbook that I opened already.
I don't know how the VBA command for that. Can anyone help?
It would be nice too if I can add the command to close the file after finished copying it.
Sub AllFiles()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
folderPath = "\\obc\Share\【Dept】\customer\" 'change to suit
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xls")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)
'Call a subroutine here to operate on the just-opened workbook --> (I don't know how to create a subroutine so I just put the command directly in it.)
Columns("A:W").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("Sheet1").Select
Columns("A:W").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Sheets("Print").Select
ActiveSheet.PrintOut
filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
I have found a VBA code in mrexcel from previous years before in regard to opening all files in a certain folder.
Below are the code and I modified it a bit.
The VBA below is supposed to go open a file in a certain folder. Copy it to another opened workbook, go to another sheet in that workbook and print it out.
Next go to the next file in that folder and repeat the task.
The thing is that I also need to put the file name that I just opened in cell W2 so that I can copy it together to the workbook that I opened already.
I don't know how the VBA command for that. Can anyone help?
It would be nice too if I can add the command to close the file after finished copying it.
Sub AllFiles()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
folderPath = "\\obc\Share\【Dept】\customer\" 'change to suit
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xls")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)
'Call a subroutine here to operate on the just-opened workbook --> (I don't know how to create a subroutine so I just put the command directly in it.)
Columns("A:W").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("Sheet1").Select
Columns("A:W").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Sheets("Print").Select
ActiveSheet.PrintOut
filename = Dir
Loop
Application.ScreenUpdating = True
End Sub