尝试将表复制到csv文件并使用原始文件名命名

时间:2017-10-19 16:23:15

标签: excel-vba vba excel

我是VBA的新手并且遇到了问题。我正在尝试将表复制到.csv文件,我希望最终结果包含原始.xlsm名称&表名和日期/时间。我已成功拼凑代码将表格导出到.csv表格和日期/时间,但我很难在那里获得文件鬃毛。我收到以下错误“对象'_Workbook'的方法'SaveAs'失败” 以下是我所拥有的,任何帮助都会很棒!

Sub ExportTableBanquetEarnings()
 Application.ScreenUpdating = False

 Sheets("BanquetEarnings").Visible = True
 Sheets("BanquetEarnings").Select

Dim wb As Workbook, wbNew As Workbook
Dim ws As Worksheet, wsNew As Worksheet
Dim wbNewName As String
Dim wbCurrent As String

wbCurrent = ThisWorkbook.FullName

Set wb = ThisWorkbook
Set ws = ActiveSheet

Set wbNew = Workbooks.Add


With wbNew
   Set wsNew = wbNew.Sheets("Sheet1")
   wbNewName = ws.ListObjects(1).Name
   ws.ListObjects(1).Range.Copy
   wsNew.Range("A1").PasteSpecial Paste:=xlPasteAll
   .SaveAs Filename:="F:\admin\Report Databases\BanquetTipouts" & "\" & 
    wbCurrent & wbNewName & Format(Now, "yyyymmdd_hhmm") & ".csv", _
         FileFormat:=xlCSVMSDOS, CreateBackup:=False
 End With

 Application.DisplayAlerts = False
 ActiveWorkbook.Close
 Application.DisplayAlerts = True

 ActiveWorkbook.RefreshAll

 Sheets("BanquetEarnings").Visible = False
 Sheets("Blank Cost Sheet").Select
 Workbooks.Open "F:\Function Agreements\Cost Sheets M\Payroll Report - 
   V2.xlsm"

End Sub

1 个答案:

答案 0 :(得分:0)

以下代码将显示该工作簿的路径

wbCurrent = ThisWorkbook.FullName

我认为你打算用的是......

wbCurrent = ThisWorkbook.Name

另外,我认为另一个问题可能是Now方法,它应该是Now()