Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. Private Sub Export_Click()
  2.  
  3. Application.ScreenUpdating = False
  4. Application.DisplayAlerts = False
  5.  
  6. Dim MyPath As String
  7. Dim MyFileName As String
  8. 'The path and file names:
  9. MyPath = ActiveWorkbook.Path & "Uploads"
  10. MyFileName = "" & Range("a2") & "_Upload"
  11.  
  12. On Error GoTo Ending
  13.  
  14. 'Makes sure the path name ends with "":
  15. If Not Right(MyPath, 1) = "" Then MyPath = MyPath & ""
  16. 'Makes sure the filename ends with ".csv"
  17. If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
  18. 'Copies the sheet to a new workbook:
  19. Sheets("UploadData").Copy
  20. 'The new workbook becomes Activeworkbook:
  21. With ActiveWorkbook 'Saves the new workbook to given folder / filename:
  22. .SaveAs FileName:= _
  23. MyPath & MyFileName, _
  24. FileFormat:=xlCSV, _
  25. CreateBackup:=False 'Closes the file
  26. .Close False
  27. End With
  28.  
  29. ChDir MyPath
  30. Workbooks.Open FileName:= _
  31. MyPath & "" & MyFileName & """"
  32.  
  33. ActiveWorkbook.Save
  34. ActiveWorkbook.Close
  35.  
  36. GoTo Skip
  37.  
  38. Ending:
  39. MsgBox ("ERROR! Please make sure you have a folder named Uploads next to the template file")
  40. ActiveWorkbook.Close
  41.  
  42. Application.DisplayAlerts = True
  43. Application.ScreenUpdating = True
  44.  
  45. Skip:
  46. Application.DisplayAlerts = True
  47. Application.ScreenUpdating = True
  48. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement