我必须复制数据并将其粘贴到新工作表中,并根据用户要求更改工作表名称,并在所需位置使用相同名称保存。我已编写代码并已执行但我找不到指定的文件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
答案 0 :(得分:2)
您没有保存任何内容,只需获取文件的名称即可保存并显示消息框。
If saveasfile <> "False" Then
ActiveSheet.move ' <-- Add this line
ActiveSheet.SaveAs saveasfile, xlCSV ' <-- Add this line
MsgBox "saved as " & saveasfile
End If