简体   繁体   English

Excel中的VBA代码

[英]VBA Code in Excel

I have this code, although it states there is an error. 我有这段代码,尽管它指出存在错误。 What i want to do is complete csv file export from the current active sheet, and save it with the information currently in cell A2 . 我想做的是从当前活动工作表中完整地导出csv文件,并将其与当前存储在单元格A2的信息一起保存。

Sub exportCSV()
     ' export Macro

    Range("A:F").Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    strName = AprilPayslips.Range("A2")
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & strName
    , FileFormat:=xlCSV, CreateBackup:=False
    Application.DisplayAlerts = False
    Application.DisplayAlerts = True
End Sub

If you want the value of a cell as a string then ask for it :) 如果您希望将单元格的值作为字符串,则要求它:)

strName = Range("A2").Value

These two lines won't do anything as they stand, so remove them: 这两行将无法执行任何操作,因此请将其删除:

Application.DisplayAlerts = False
Application.DisplayAlerts = True

Paths should always include the trailing slash so you don't need to add that in: 路径应始终包含斜杠,因此您无需在其中添加:

Sub exportCSV()
    strName = Range("A2").Value
    Range("A:F").Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & strName, FileFormat:=xlCSV, CreateBackup:=False
End Sub

Should work for you, however unless you can be sure that the contents of A2 will always be a valid filename you may run into problems unless you add in some extra validation. 应该为您工作,但是,除非您可以确定A2的内容始终是有效的文件名,否则您可能会遇到问题,除非您添加一些额外的验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM