如何在特定文件夹中保存文件

时间:2015-04-29 07:51:49

标签: vba excel-vba excel

我一直在使用代码将vba文件中的数据粘贴到另一个文件中。我粘贴数据的文件的名称始终是“endofdaydividends”,我需要每天在同一个地方保存此文件并替换旧文件。我想保存在我的桌面上。

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

使用此代码,我关闭宏文件并仅打开“endofdaydividends”文件。有谁知道如何添加命令将其保存在我的文件夹中?我需要保存在这个地址中 T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv

1 个答案:

答案 0 :(得分:0)

你能试试吗?

删除以前文件的代码,需要在粘贴新值之前添加到函数的开头:

SetAttr "T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv", vbNormal
Kill "T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv"

保存新文件的代码:

ActiveWorkbook.SaveAs Filename:="T:\CTG\EDM\Current EDM LDN\MacroEODDividend\endofdaydividends.csv", FileFormat:=xlCSV, CreateBackup:=False

更新后的代码:您的代码应如下所示。我们在这里做的是,我们首先删除现有的CSV文件,然后创建一个新文件并将活动工作表重命名为您想要的,然后粘贴所需的值并将新创建的工作簿保存为新的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