Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. Sub ImportOldRates()
  2.  
  3. 'Declare the variables
  4. Dim MyPath As String
  5. Dim MyFile As String
  6. Dim LatestFile As String
  7. Dim LatestDate As Date
  8. Dim LMD As Date
  9.  
  10. 'Specify the path to the folder
  11. MyPath = "C:Folder1Folder2"
  12.  
  13. 'Make sure that the path ends in a backslash
  14. If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
  15.  
  16. 'Get the first Excel file from the folder
  17. MyFile = Dir(MyPath & "*.xls", vbNormal)
  18.  
  19. 'If no files were found, exit the sub
  20. If Len(MyFile) = 0 Then
  21. MsgBox "No files were found...", vbExclamation
  22. Exit Sub
  23. End If
  24.  
  25. 'Loop through each Excel file in the folder
  26. Do While Len(MyFile) > 0
  27.  
  28. 'Assign the date/time of the current file to a variable
  29. LMD = FileDateTime(MyPath & MyFile)
  30.  
  31. 'If the date/time of the current file is greater than the latest
  32. 'recorded date, assign its filename and date/time to variables
  33. If LMD > LatestDate Then
  34. LatestFile = MyFile
  35. LatestDate = LMD
  36. End If
  37.  
  38. 'Get the next Excel file from the folder
  39. MyFile = Dir
  40.  
  41. Loop
  42.  
  43. 'Open the latest file
  44. Workbooks.Open MyPath & LatestFile
  45. Application.DisplayAlerts = False
  46. Application.EnableEvents = False
  47. Application.Run "ConnectChartEvents"
  48. Cells.Select
  49. Range("E2").Activate
  50. Selection.copy
  51. ActiveWindow.Close
  52. ActiveSheet.PasteSpecial Format:="Unicode Text", Link:=False, _
  53. DisplayAsIcon:=False, NoHTMLFormatting:=True
  54. Application.CutCopyMode = False
  55. Selection.Columns.AutoFit
  56. Range("A1").Select
  57. Application.DisplayAlerts = True
  58. Application.EnableEvents = True
  59.  
  60. End Sub
  61.  
  62. Application.Wait (Now + TimeValue("0:00:01"))
  63.  
  64. Range("E2").Activate
  65.  
  66. Range("A1,Z400").Activate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement