2012-12-17 87 views
0

我有兩個.vbs文件,說a.vbs和b.vbs.Now都是爲同一個Excel編寫的,但可以在兩張不同的工作表上運行。所以我們可以運行那些平行?要並行運行兩個或多個.VBS腳本

EDIT

a.vbs將更新Sheet 2中和b.vbs將更新sheet3.But兩個源片是Sheet 1中。

請指點如何設置這樣的環境

代碼

Option Explicit 

Dim objExcel1 
Dim strPathExcel1 
Dim objSheet1,objSheet2 
Dim IntRow1,IntRow2 
Dim ColStart 

Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump 

strPathExcel1 = "D:\AravoVB\Copy of Original  Scripts\CopyofGEWingtoWing_latest_dump_21112012.xls" 
objExcel1.Workbooks.open strPathExcel1 
Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets(1) 
Set objSheet2 = objExcel1.ActiveWorkbook.Worksheets("Bad Data") 

objExcel1.ScreenUpdating = False 
objExcel1.Calculation = -4135 'xlCalculationManual 

IntRow2=2 
IntRow1=4 
Do Until IntRow1 > objSheet1.UsedRange.Rows.Count 

    ColStart = objExcel1.Application.WorksheetFunction.Match("Parent Business Process ID", objSheet1.Rows(3), 0) + 1 

Do Until ColStart > objSheet1.UsedRange.Columns.Count And objSheet1.Cells(IntRow1,ColStart) = "" 

    If objSheet1.Cells(IntRow1,ColStart + 1) > objSheet1.Cells(IntRow1,ColStart + 5) And objSheet1.Cells(IntRow1,ColStart + 5) <> "" Then 

    objSheet1.Range(objSheet1.Cells(IntRow1,1),objSheet1.Cells(IntRow1,objSheet1.UsedRange.Columns.Count)).Copy 
    objSheet2.Range(objSheet2.Cells(IntRow2,1),objSheet2.Cells(IntRow2,objSheet1.UsedRange.Columns.Count)).PasteSpecial 
    IntRow2=IntRow2+1 
    Exit Do 

    End If 

ColStart=ColStart+4 
Loop 

IntRow1=IntRow1+1 
Loop 

objExcel1.ScreenUpdating = True 
objExcel1.Calculation = -4105 'xlCalculationAutomatic 

代碼B

Option Explicit 

Dim objExcel1 
Dim strPathExcel1 
Dim objSheet1,objSheet2 
Dim IntRow1,IntRow2 
Dim Flag 
Dim IntColTemp,IntRowTemp 
Dim Strcmp1,Strcmp2 

Flag=0 
IntColTemp=1 
IntRowTemp=3 

    Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump 

If Err.Number <> 0 Then 
    On Error GoTo 0 
    Wscript.Echo "Excel application not found." 
    Wscript.Quit 
End If 

strPathExcel1 = "D:\VA\CopyofGEWingtoWing_latest_dump_21112012.xls" 
    objExcel1.Workbooks.open strPathExcel1 

Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets(1) 
Set objSheet2 = objExcel1.ActiveWorkbook.Worksheets(2) 

IntRow1=4 
IntRow2=1 

Do While objSheet1.Cells(IntRow1, 1).Value <> "" 

    objSheet2.Cells(IntRow2, 1).Value = objSheet1.Cells(IntRow1, 1).Value 


IntColTemp=1 
Flag=0 
'This will travarse to the Parent Business Process ID column horizantally in the excel. 
Do While Flag=0 

    If objSheet1.Cells(IntRowTemp,IntColTemp).Value="Parent Business Process ID" Then 

     Flag=1  

    End If 

     IntColTemp=IntColTemp+1 


Loop 
     IntColTemp=IntColTemp-1 
     'MsgBox(IntColTemp) 

    Strcmp1=trim(objSheet1.Cells(IntRow1, 1).Value) 
    Strcmp2=trim(objSheet1.Cells(IntRow1,IntColTemp).Value) 

    If Strcmp1=Strcmp2 Then 

     objSheet2.Cells(IntRow2, 2).Value="Parent" 

    Else 

     objSheet2.Cells(IntRow2, 2).Value="child" 

    End If 


    IntRow1=IntRow1+1 
    IntRow2=IntRow2+1 

    Loop 
+0

我想他們需要從Excel以外的地方調用,因爲Excel在VBA/VB6上運行,默認情況下它不支持多線程。你能否提供更多關於每個腳本對每張表做什麼的細節,甚至可能發佈代碼,我們可能會提供另一種解決方案? –

+0

當然,我會的。請參閱更新。 –

+0

你能否檢查我的更新? –

回答

1

在兩個不同的片s工作HOULD通過把這樣的事情在這兩個腳本的可能:

strPathExcel1 = "D:\CopyofGEWingtoWing_latest_dump_21112012.xls" 

On Error Resume Next 
Set objExcel1 = GetObject(, "Excel.Application") ' attach to running instance 
If Err.Number = 429 Then       ' if that fails 
    Err.Clear 
    Set objExcel1 = CreateObject("Excel.Application") ' create new instance 
    If Err Then          ' if that still fails 
    WScript.Echo Err.Description & " (0x" & Hex(Err.Number) & ")" 
    WScript.Quit 1         ' report error and terminate 
    End If 
    objExcel1.Workbooks.Open strPathExcel1 
End If 
On Error Goto 0 

然而,我懷疑這種方法會得到你的性能足以證明額外的複雜性。

代碼更換線

Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump 

strPathExcel1 = "D:\AravoVB\Copy of Original Scripts\CopyofGEWingtoWing_latest_dump_21112012.xls" 
objExcel1.Workbooks.open strPathExcel1 

與上面的代碼塊。

代碼B更換線

Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump 

If Err.Number <> 0 Then 
    On Error GoTo 0 
    Wscript.Echo "Excel application not found." 
    Wscript.Quit 
End If 

strPathExcel1 = "D:\VA\CopyofGEWingtoWing_latest_dump_21112012.xls" 
objExcel1.Workbooks.open strPathExcel1 

與上面的代碼塊。

+0

「On Error Goto 0」這個聲明做了什麼?我應該把這個代碼放在我的兩個代碼的頂部嗎? –

+1

'On Error Resume Next'在VBScript中啓用錯誤「處理」,'On Error Goto 0'會禁用它(請參閱[文檔](http://msdn.microsoft.com/zh-cn/library/53f3k80h%28v= vs.84%29.aspx)瞭解詳情)。我的答案中的代碼片段應該替換腳本中的Set objExcel1 = CreateObject(「Excel.Application」)行。不過,您需要調整變量名稱。 –

+0

我明白你在說什麼,但是想知道代碼的哪一部分我必須放置錯誤處理塊?在最後的代碼? –