将工作表另存为.CSV文件

时间:2017-04-20 10:22:17

标签: excel vba excel-vba

我必须复制数据并将其粘贴到新工作表中,并根据用户要求更改工作表名称,并在所需位置使用相同名称保存。我已编写代码并已执行但我找不到指定的文件location.Please帮我这个。

Sub saveascsv()

Dim Rng As Range
Dim filenam As Variant
Dim saveasfile As Variant

filenam = InputBox("Enter name of the file to be saved")

Set Rng = Range("E1:H" & Range("H" & Rows.Count).End(xlUp).Row)
Rng.Select
Selection.Copy

ActiveWorkbook.Sheets.Add after:=Worksheets("Part_Number")
ActiveSheet.Name = filenam
ActiveSheet.Paste

ActiveSheet.Columns("A:D").AutoFit

Application.CutCopyMode = False

saveasfile = Application.GetSaveAsFilename(InitialFileName:=filenam, 
FileFilter:="CSV (Comma delimited) (*.csv), *.csv", Title:="Save As")

If saveasfile <> "False" Then
   MsgBox "saveas " & filenam
End If

End Sub

1 个答案:

答案 0 :(得分:2)

您没有保存任何内容,只需获取文件的名称即可保存并显示消息框。

If saveasfile <> "False" Then
    ActiveSheet.move                     ' <-- Add this line
    ActiveSheet.SaveAs saveasfile, xlCSV ' <-- Add this line
    MsgBox "saved as " & saveasfile
End If