Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. Private Sub workbook_open()
  2. Application.Visible = False
  3.  
  4. Dim wb As Workbook
  5. Set wb = ActiveWorkbook
  6.  
  7. Call Copy_FilesImport
  8.  
  9. Application.Visible = True
  10. Application.Quit
  11. Application.ActiveWindow.Close savechanges:=True
  12. ActiveWorkbook.Close savechanges:=True
  13. End Sub
  14. Private Sub Copy_FilesImport()
  15. Dim fso1 As Object
  16. Dim FromPath As String
  17. Dim ToPath As String
  18. Dim FileExt As String
  19.  
  20. FromPath = "I:Path1"
  21. ToPath = "I:Path2"
  22.  
  23. Dim FilName As String
  24. FilName = "FileA.xlsx"
  25.  
  26. If Right(FromPath, 1) <> "" Then
  27. FromPath = FromPath & ""
  28. End If
  29.  
  30. Set fso1 = CreateObject("scripting.filesystemobject")
  31.  
  32. If fso1.FolderExists(FromPath) = False Then
  33. MsgBox FromPath & "Error message"
  34. Exit Sub
  35. End If
  36.  
  37. If fso1.FolderExists(ToPath) = False Then
  38. MsgBox ToPath & "Error message"
  39. Exit Sub
  40. End If
  41.  
  42. fso1.copyFile Source:=FromPath & FilName, Destination:=ToPath
  43. Call Renamefiles1
  44. End Sub
  45.  
  46. Private Sub Renamefiles1()
  47.  
  48. Dim sName As String
  49. Dim fso As Object
  50. Dim fol As Object
  51. Set fso = VBA.CreateObject("Scripting.FileSystemObject")
  52. Set fol = fso.GetFolder("I:Path2")
  53.  
  54. For Each Fil In fol.Files
  55. If InStr(1, Fil.Name, "FileA.xlsx") <> 0 Then
  56. sName = Replace(Fil.Name, "FileA", "FileD")
  57. Fil.Name = sName
  58. End If
  59. Next
  60.  
  61. Call Value
  62.  
  63. End Sub
  64.  
  65. Private Sub Value()
  66. Workbooks.Open Filename:=("I:Path2FileD.xlsx")
  67. Dim file As String
  68. file = "FileD.xlsx"
  69. For i = 1 To Workbooks(sName).Worksheets.Count
  70.  
  71. Sheets(i).Select
  72.  
  73. Range("A1").Select
  74. Sheets(i).Cells.Select
  75. Selection.Copy
  76. Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats,
  77. Operation:=xlNone, SkipBlanks _
  78. :=False, Transpose:=False
  79.  
  80. Next i
  81. Workbooks("FileD.xlsx").Close savechanges:=True
  82.  
  83. Call Renamefiles2
  84.  
  85. End Sub
  86.  
  87.  
  88.  
  89. Private Sub Renamefiles2()
  90.  
  91. Dim sName As String
  92. Dim fso As Object
  93. Dim fol As Object
  94. Set fso = VBA.CreateObject("Scripting.FileSystemObject")
  95. Set fol = fso.GetFolder("I:Path2")
  96.  
  97. Dim datoformat As String
  98. datoformat = Format(Date, "yyyy-mm-dd")
  99.  
  100.  
  101. For Each Fil In fol.Files
  102. If InStr(1, Fil.Name, "FileD.xlsx") <> 0 Then
  103. sName = Replace(Fil.Name, "FileD", "FileD_" & datoformat)
  104. Fil.Name = sName
  105. End If
  106. Next
  107. MsgBox "Succes"
  108. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement