Excel VBA打开工作簿,执行操作,另存为,closures
这个问题已经被编辑,由于冗长的评论和提出的答案更新。
这里要求的是模块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
还有error handling的问题,我知道我错了,但我更有兴趣修复closuresfunction,在我进入它的那一刻。 这是需要一些工作的error handling代码
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
这是你的testing代码加上我改变的文件path和名称
讨论后发布更新的答案:
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
error handling:
你可以尝试这样的事情来捕捉一个特定的错误:
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
我会尝试回答几个不同的事情,但是我的贡献可能不包括你所有的问题。 也许我们中的几个人可以采取不同的块。 但是,这个信息应该对你有帮助。 开始了..
打开一个单独的文件:
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
打开指定date的文件如果存在:
我不知道如何search你的目录,看看是否存在一个文件,但在我的情况下,我不打扰search它,我只是试图打开它,并进行了一些错误检查,如果它不然后显示这个消息或做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
或者(更好的select):
如果不存在,则popup一个消息框或对话框,说“该文件不存在,你想创build一个新的?
你很可能会想要使用下面显示的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
有关error handling的更多信息,请访问: http : //www.cpearson.com/excel/errorhandling.htm
另外如果你想了解更多或需要更多地了解VBA,我会推荐Siddharth Rout的网站,他有很多的教程和示例代码在这里: http : //www.siddharthrout.com/vb-dot-net-and- Excel中/
希望这可以帮助!
有关如何确保错误代码不会运行EVERYtime的示例:
如果你通过代码debugging没有Exit Sub
之前的error handling程序,你会很快意识到error handling程序将运行每次如果有一个错误regarldess或不。 代码示例下面的链接显示了此问题的上一个答案。
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块不工作的VBA