2012-10-18 173 views
8

由于冗长的评论和来自提出的答案的更新,此问题已被编辑。Excel VBA打开工作簿,执行操作,另存为,关闭

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

还有一些与ErrorHandling中的问题,我知道我出了毛病,但我更感兴趣的是,此刻固定关闭功能之前,我进去。下面是需要一些工作

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

好,我只认为这是你的问题:“总的来说,我想在我的窗体打开某些工作簿,要么执行某些操作的按钮的功能,以一种格式保存,然后关闭或打开工作簿并显示信息。“ - 这些都是serperate任务,这些任务很容易在excel帮助或网络中获得解决方案 - 您的问题在这里出现了一些问题,比如“请相信,做我的工作” - 将其分解为更多与代码相关的问题。 – Jook

回答

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 
+0

如果它不再打开,删除'owb.close'行。如果要在保存后关闭它,请添加另一行以引用新工作簿的名称。 –

+0

是的,同样的方法可以应用,如:'.SaveAs mypath&“\”&yr&mnth' –

+0

如果我们稍微回溯一下,是否将'oWB'声明为工作簿并将其设置为基于打开的工作簿从组合框派生的值?如果我理解你,然后在打开的此工作簿上执行一些更多操作,并将其另存为另一个文件名(YYYYMMDB.xlsx)?如果是这样,那么你应该仍然可以使用'oWB'变量来关闭工作簿。 –

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 

或(更好的选择):

if one doesn't exist then bring up either a message box or dialogue box to say "the file does not exist, would you like to create a new one?

你最有可能想用下面所示的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溃败的网站,他有很多的教程和示例代码在这里: 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


+0

谢谢,不幸的是我现在要离开工作,但我明天会看看这个 – JamesDev

+0

听起来不错,期待听到你的声音 –

+0

如果尝试打开文件失败,我已经使用错误处理来显示一条消息。不幸的是,当文件打开工程,我仍然收到消息 – JamesDev

相关问题