Excel VBA打开工作簿,执行操作,另存为,关闭

时间:2012-10-18 09:57:24

标签: excel vba excel-vba automation userform

由于评论的冗长和建议答案的更新,此问题已经过编辑。

这里要求的是模块13;

Sub SaveInFormat()
Application.DisplayAlerts = False
Workbooks.Application.ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\jammil\Desktop\AutoFinance\ProjectControl\Data\" & Format(Date, "yyyymm") & "DB" & ".xlsx",   leFormat:=51
Application.DisplayAlerts = True
End Sub

此外还有错误处理的问题,我知道我已经出错了,但是在我进入它之前,我对修复关闭功能更感兴趣。这是需要一些工作的错误处理代码

Sub test()

Dim wk As String, yr As String, fname As String, fpath As String
Dim owb As Workbook

wk = ComboBox1.Value
yr = ComboBox2.Value
fname = yr & "W" & wk
fpath = "C:\Documents and Settings\jammil\Desktop\AutoFinance\ProjectControl\Data"
owb = Application.Workbooks.Open(fpath & "\" & fname)
On Error GoTo ErrorHandler:
ErrorHandler:
If MsgBox("This File Does Not Exist!", vbRetryCancel) = vbCancel Then Exit Sub Else Call Clear

'Do Some Stuff

Call Module13.SaveInFormat

owb.Close

这是您的测试代码加上我更改的文件路径和名称

2 个答案:

答案 0 :(得分:8)

讨论后发布更新答案:

Option Explicit
Sub test()

    Dim wk As String, yr As String
    Dim fname As String, fpath As String
    Dim owb As Workbook

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    wk = ComboBox1.Value
    yr = ComboBox2.Value
    fname = yr & "W" & wk
    fpath = "C:\Documents and Settings\jammil\Desktop\AutoFinance\ProjectControl\Data"

    On Error GoTo ErrorHandler
    Set owb = Application.Workbooks.Open(fpath & "\" & fname)

    'Do Some Stuff

    With owb
        .SaveAs fpath & Format(Date, "yyyymm") & "DB" & ".xlsx", 51
        .Close
    End With

    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
        .EnableEvents = True
    End With

Exit Sub
ErrorHandler: If MsgBox("This File Does Not Exist!", vbRetryCancel) = vbCancel Then

Else: Call Clear

End Sub

错误处理:

你可以尝试这样的方法来捕捉一个特定的错误:

    On Error Resume Next
    Set owb = Application.Workbooks.Open(fpath & "\" & fname)
    If Err.Number = 1004 Then
    GoTo FileNotFound
    Else
    End If

    ...
    Exit Sub
    FileNotFound: If MsgBox("This File Does Not Exist!", vbRetryCancel) = vbCancel Then

    Else: Call Clear

答案 1 :(得分:2)

我会尝试回答几个不同的事情,但是我的贡献可能不会涵盖你的所有问题。也许我们中的几个人可以采取不同的块。但是,此信息应该对您有所帮助。我们走了......

打开单独的文件:

ChDir "[Path here]"                          'get into the right folder here
Workbooks.Open Filename:= "[Path here]"      'include the filename in this path

'copy data into current workbook or whatever you want here

ActiveWindow.Close                          'closes out the file

打开具有指定日期的文件(如果存在):

我不确定如何搜索您的目录以查看文件是否存在,但在我的情况下我不打算搜索它,我只是尝试打开它并进行一些错误检查以便如果它不存在则显示此消息或执行xyz。

一些常见的错误检查语句:

On Error Resume Next   'if error occurs continues on to the next line (ignores it)

ChDir "[Path here]"                         
Workbooks.Open Filename:= "[Path here]"      'try to open file here

或(更好的选择):

  

如果不存在,则显示消息框或对话框   框说“文件不存在,你想创建一个新的   一个?

您很可能希望使用下面显示的GoTo ErrorHandler来实现此目标

On Error GoTo ErrorHandler:

ChDir "[Path here]"                         
Workbooks.Open Filename:= "[Path here]"      'try to open file here

ErrorHandler:
'Display error message or any code you want to run on error here

有关此处错误处理的更多信息:http://www.cpearson.com/excel/errorhandling.htm


此外,如果你想了解更多或者需要更多地了解VBA,我会推荐Siddharth Rout的网站,他有很多教程和示例代码: http://www.siddharthrout.com/vb-dot-net-and-excel/

希望这有帮助!


有关如何确保错误代码无法每天运行的示例:

如果您在没有Exit Sub错误处理程序的情况下通过代码进行调试,您很快就会意识到错误处理程序将在每次重新运行时运行,如果有错误的话。代码示例下方的链接显示了此问题的上一个答案。

  Sub Macro

    On Error GoTo ErrorHandler:

    ChDir "[Path here]"                         
    Workbooks.Open Filename:= "[Path here]"      'try to open file here

    Exit Sub      'Code will exit BEFORE ErrorHandler if everything goes smoothly
                  'Otherwise, on error, ErrorHandler will be run

    ErrorHandler:
    'Display error message or any code you want to run on error here

  End Sub

另外,看看这个其他问题你需要更多参考这是如何工作的: goto block not working VBA