10

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

这里要求的是模块 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

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

4

2 回答 2

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
于 2012-10-18T13:25:17.037 回答
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.siddharthout.com/vb-dot-net-and-优秀/

希望这可以帮助!


有关如何确保错误代码不会每次都运行的示例:

如果您在没有Exit SubBEFORE 错误处理程序的情况下通过代码进行调试,您很快就会意识到错误处理程序每​​次都会运行,无论是否有错误。代码示例下方的链接显示了此问题的先前答案。

  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


于 2012-10-18T13:07:25.070 回答