VBA复制方法一直失败?

时间:2017-10-19 15:28:07

标签: excel vba excel-vba

我本可以发誓之前这是有效的 - 但由于某种原因,这似乎不再起作用了。我正在尝试使用活动工作表(同样,这可能不是很漂亮或干净,但我仍然是VBA的新手),将其复制到新工作表,在新工作表中我要打开Excel另存为对话框,当工作表保存(CSV)格式时,我希望工作簿关闭(或者即使它没有关闭)至少将用户返回到原始工作簿并结束子

Sub saveExportAs()
    Application.CutCopyMode = False
    Sheets("load").Select
    ActiveWorkbook.Activate
    Sheets("load").Copy
    Dim varResult As Variant
    Dim ActBook As Workbook
    'display the save as dialog
    varResult = Application.GetSaveAsFilename(InitialFileName:="\\network\folder\upload_" & Format(Date, "yyyy-mm-dd") & ".csv", FileFilter:= _
    "Comma Delimited / Upload Files (*.csv),*.csv", Title:="Save Upload File")
    'check to make sure the user didn't cancel
    If varResult <> False Then
        ActiveWorkbook.saveAs Filename:=varResult, _
        FileFormat:=xlCSV
        Exit Sub
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

您可以使用定义为工作簿/工作表的工作表来避免问题......可能是这样的:

Sub saveExportAs()
Dim wb1, wb2 As Workbook
Dim ws As Worksheet
Dim varResult As Variant
Set wb1 = ThisWorkbook
Set ws = ThisWorkbook.Worksheets("load")
ws.Copy
Set wb2 = ActiveWorkbook
varResult =     Application.GetSaveAsFilename(InitialFileName:="\\network\folder\upload_" & Format(Date, "yyyy-mm-dd") & ".csv", FileFilter:= _
"Comma Delimited / Upload Files (*.csv),*.csv", Title:="Save Upload File")
    If varResult <> False Then
        wb2.SaveAs Filename:=varResult, FileFormat:=xlCSV
        wb2.Close Savechanges:=True
        Exit Sub
    End If
wb1.Activate
End Sub

答案 1 :(得分:0)

试试这个......

JavaSparkContext