我慢慢學習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萬。
你所要求的聲音,像一個'Class' - [繼承人好樣的開始你的研究 - CPearson.com(http://www.cpearson.com/Excel/Classes.aspx) –