I have been working in a code to paste the data from the vba file to another file. The name of the file which i paste the data is always "endofdaydividends" and i need to save this file everyday in the same place and replace the old one. I would like to save in my desktop.
Sub move_to_csvfile()
Dim x As Workbook
Dim y As Workbook
Dim strPath As String
Dim fileName As String
Dim dd As String
Set x = ThisWorkbook
Set y = Workbooks.Open("T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv")
Application.DisplayAlerts = False
x.Sheets("Code").Range("A:F").EntireColumn.Copy
Application.DisplayAlerts = True
Application.DisplayAlerts = False
y.Sheets("endofdaydividends").Range("A1").PasteSpecial
Application.DisplayAlerts = True
x.Close False
ActiveWorkbook.Save
End Sub
With this code, i close the macro file and leave opened only the "endofdaydividends" file. Does anyone know how to add a command to save it on my folder? I need to save in this address T:\\CTG\\EDM\\Current EDM LDN\\MacroEODDividend\\endofdaydividends.csv
Can you please try following?
Code to delete your previous file, need to add to beginning of your function before pasting the new value:
SetAttr "T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv", vbNormal
Kill "T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv"
Code to save the new file:
ActiveWorkbook.SaveAs Filename:="T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv", FileFormat:=xlCSV, CreateBackup:=False
UPDATED CODE: Your Code should look like this. What we do here, we first delete existing CSV file then, create a new file and rename the active sheet to what you desire then paste required values and save the new created workbook as new CSV.
Sub move_to_csvfile()
Dim x As Workbook
Dim y As Workbook
Dim strPath As String
Dim fileName As String
Dim dd As String
Set x = ThisWorkbook
On Error Resume Next
SetAttr "T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv", vbNormal
Kill "T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv"
On Error GoTo 0
Set y = Workbooks.Add
y.ActiveSheet.Name = "endofdaydividends"
Application.DisplayAlerts = False
x.Sheets("Code").Range("A:F").EntireColumn.Copy
Application.DisplayAlerts = True
Application.DisplayAlerts = False
y.Sheets("endofdaydividends").Range("A1").PasteSpecial
Application.DisplayAlerts = True
y.SaveAs fileName:="T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv", FileFormat:=xlCSV, CreateBackup:=False
x.Close False
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.