父母和子女处理隔离和子列表

时间:2012-12-12 08:36:47

标签: excel vbscript

请阅读以下模板:

PID     Status      LPID

10       Closed      25
11       Open        25
31       Open        31
25       Closed      25
54       Open        31
17       Open        17
20       Closed      31
88       closed      77
77       closed      77

现在当PID!= LPID时,该PID被定义为CPID(子进程ID),否则它是PPID(父进程ID)

现在我正在寻找一个代码,它将告诉哪个是父级,哪个是子级 - 意味着在另一个工作表中标记它们。同时我想列出所有CPID,PPID在同一行,如果有任何PPID让孩子自己处理。输出将如下所示

PID   Type Of Process?    Child List
10       Child
11       Child
31       Parent              54 20
25       Parent              10 11
54       Child
17       Parent
20       Child
88       Child
77       Parent              88

我已经使用VBScript编写了一个代码,但实际的工作表太慢了。对于2500个数据,它需要接近1小时。所以我想要比我的更快的过程。

你能帮忙使用VBscript吗?

代码1:

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


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

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

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

    IntRow2=4
    IntChildListColumn=3

    If objSheet1.Cells(IntRow1,2).Value="Parent" Then

        Do While objSheet2.Cells(IntRow2, 1).Value <> ""

             If objSheet2.Cells(IntRow2,11).Value=objSheet1.Cells(IntRow1,1).Value And objSheet2.Cells(IntRow2,11).Value <> objSheet2.Cells(IntRow2,1).Value Then

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

             End If

      IntRow2=IntRow2+1

      Loop

   End If

 IntRow1=IntRow1+1

Loop

代码2:

 Flag=0
 IntColTemp=1
 IntRowTemp=3

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


 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

1 个答案:

答案 0 :(得分:1)

一些建议的改进:

如果你确实打开了excel的屏幕:在代码开头设置Application.ScreenUpdating = False(以及True和结束)

将Application.Calculation设置为手动。在您的代码开始时: Application.Calculation = xlCalculationManual,最后回到自动:Application.Calculation = xlCalculationAutomatic。如果这本身不是自动的,您也可以先将原始设置存储到本地变量中,并在最后恢复该值。

使用VBA查看性能是否更好(如果VBA对您有效)

使用Value2代替Value,此属性的查询速度更快。

减少循环次数,尤其是在查找某个范围内的特定值时。 您也可以使用Excel Lookup&amp;参考函数(例如VLookup)可能会更有效,更快速地运行。