2012-10-18 175 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

相關問題