2017-02-20 158 views
0

我慢慢學習VBA,並且已經實現了一些本質上從論壇切割和粘貼的好東西,但現在我卡住了。重複使用VBA Sub打開excel文件

在此處使用此腳本:WordMVP我可以從Word中打開excel文件。沒問題。但是現在我已經重複使用了這個腳本,好幾次我的模塊開始變長了。我可以把它分成不同的潛艇嗎?當我需要它們時調用零件?如:(黑客鏈接的代碼位)

在一個獨立的子將這個:

Sub WorkOnAWorkbook(str As String) 

    Dim oXL As Excel.Application 
    Dim oWB As Excel.Workbook 
    Dim oSheet As Excel.Worksheet 
    Dim oRng As Excel.Range 
    Dim ExcelWasNotRunning As Boolean   

    'If Excel is running, get a handle on it; otherwise start a new instance of Excel 
    On Error Resume Next 
    Set oXL = GetObject(, "Excel.Application") 

    If Err Then 
     ExcelWasNotRunning = True 
     Set oXL = New Excel.Application 
    End If 

    On Error GoTo Err_Handler 

    'Open the workbook 
    Set oWB = oXL.Workbooks.Open(FileName:=str) 

    Exit Sub 

    Err_Handler: 
     MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description, vbCritical, _ 
      "Error: " & Err.Number 

End Sub 

,這在其他:

Sub release() 

'Make sure you release object references. 
Set oRng = Nothing 
Set oSheet = Nothing 
Set oWB = Nothing 
Set oXL = Nothing 

End Sub 

,當我需要他們,如使用它們:

Sub test() 

Call WorkOnAWorkbook("somefile.xls") 
oWB.Application.Run "Module1.TestMacro", "JasonX" 
Call release() 

End Sub 

我在oWB.Application.Run「Module1.TestMacro」,「JasonX」中得到了一個424對象。

是這樣的可能,我的一部分東西感覺就像我快到了,我的一部分感覺我完全搞砸了,和我的一部分認爲其不可能......

任何人都可以請幫助??

Thx 100萬。

+0

你所要求的聲音,像一個'Class' - [繼承人好樣的開始你的研究 - CPearson.com(http://www.cpearson.com/Excel/Classes.aspx) –

回答

0

你不能只是分手然而代碼,你想,因爲你的對象引用需要存在您正在使用它們的子。但是,您可以使用函數來返回對象,因此可以執行一些操作,例如將用於查找/創建Excel對象的所有代碼放入返回對象的函數中。

Function OpenExcelInstance() As Excel.Application 

    Dim oXL As Excel.Application 

    'If Excel is running, get a handle on it; otherwise start a new instance of Excel 
    On Error Resume Next 
    Set oXL = GetObject(, "Excel.Application") 

    If Err Then 
     ExcelWasNotRunning = True 
     Set oXL = New Excel.Application 
    End If 

    On Error GoTo Err_Handler 

    Set OpenExcelInstance = oXL 

    Exit Function 

Err_Handler: 
    MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description, vbCritical, "Error: " & Err.Number 
    Set OpenExcelInstance = Nothing 

End Function 

然後你的測試子將減少到(雖然這代碼有沒有錯誤,如果由於某種原因未創建Excel引用或工作簿無法打開檢查)。

Sub test() 

    Dim oXL As Excel.Application 
    Dim oWB As Excel.Workbook 

    Set oXL = OpenExcelInstance() 
    Set oWB = oXL.Workbooks.Open(FileName:="somefile.xls") 

    oWB.Application.Run "Module1.TestMacro", "JasonX" 

    Set oXL = Nothing 
    Set oWB = Nothing 

End Sub 
+0

謝謝楔。它的工作原理非常完美,我最喜歡這個答案,因爲它將其他地方的許多代碼「外包」,這正是我正在嘗試做的。此外,我認爲我可以量身定製錯誤處理做更多的事情,當與其他人共享是至關重要的。否則,我要回去的是......「這行不通......」 – NotJasonStatham

0

在你的情況下,錯誤發生是因爲oWB沒有在你正在調用的sub中聲明。昏暗(聲明),調用/執行,並釋放所有需要發生在同一個子。

+0

Thx的建議,使事情更清晰。 – NotJasonStatham

1

可以縮短到這樣的事情(GetObject的打開,如果它不是已經打開的文件):

Dim oWB As Object 
Set oWB = GetObject("C:\somefile.xls", "Excel.Application") 

oWB.Application.Visible = True ' optional show the Excel Application to check the full path to the Macro 
oWB.Application.Run "Module1.TestMacro", "JasonX" 

If oWB.Application.Workbooks.Count = 1 Then oWB.Application.Quit Else oWB.Close ' optional close the Excel Application or the Workbooks 
Set oWB = Nothing