2012-12-17 273 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

我明白你在说什么,但是想知道代码的哪一部分我必须放置错误处理块?在最后的代码? –