错误:所需对象:字典对象为'[undefined]' [英] Error: Object Required: '[undefined]' for a dictionary object

查看:28
本文介绍了错误:所需对象:字典对象为'[undefined]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求:

Requirement:

请阅读以下模板:

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
101      Closed      66
102      closed      220

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

Now when PID!= LPID, that PID is defined as CPID(Child Process ID),Otherwise it is a PPID(Parent process ID)

更新可以有一些条目,例如101和102,它们是孩子,但是其父母没有自己的样式,因此不应将66,220作为出现在输出结果中66 Parent 220 Parent 应该没有这样的输出.

Update There can be some entries say 101 and 102,which are child ,but their Parents don't have a thier own etires thus 66,220 shouldn't come to the output result as 66 Parent and 220 Parent no such output should be there.

现在,我正在寻找一个代码,该代码将告诉哪个父级和哪个子级,这意味着将它们标记在另一张工作表中.同时,我想列出所有CPID,同一行中有PPID,如果有任何PPID有自己的子进程.输出如下所示

Now I am looking for a code which will tell which is parent and Which is child- means marking them in another sheet.At the Same time i want to list down all CPID,with PPID in the same row,If any PPID has child Processes themselves. Output would be look like below

PID   Type Of Process?    Child1   Child1   Child3 .... ChildN
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个小时.因此,我想要一个比我的过程更快的过程.

I have written a code using VBScript,but with the actual sheet it is too slow. For 2500 data it is taking close to 1 hour.So I Want a more faster process than my one.

我编写了以下代码来满足一些要求:但是遇到了无法理解的错误.

I wrote the below code to meet some requirement: But getting an error which is not understandable.

您能在这里帮助我吗?我以两种方式尝试了语法 Add = -没有帮助.

Can you help me here? I tried in both way the syntax Add or =- nothing helped.

显式选项

Class cP
 Public m_sRel
 Public m_dicC
    Private Sub Class_Initialize()
     m_sRel     = "Child"
     Set m_dicC = CreateObject("Scripting.Dictionary")
    End Sub
    Public Function show()
     show = m_sRel & " " & Join(m_dicC.Keys)
    End Function
End Class

Dim objSheet1,objSheet2,TotalRows,TotalcolCopy,strPathExcel1
Dim oXls  : Set oXls = CreateObject("Excel.Application")
Dim dicP  : Set dicP = CreateObject("Scripting.Dictionary")
Dim nRow,nP

strPathExcel1 = "D:\Finalscripts\test.xlsx"
oXls.Workbooks.open strPathExcel1
'oXls.Workbooks.Open(oFs.GetAbsolutePathName("Test.xlsx"))
Set objSheet1 = oXls.ActiveWorkbook.Worksheets("A")
Set objSheet2 = oXls.ActiveWorkbook.Worksheets("B")


TotalRows=oXls.Application.WorksheetFunction.CountA(objSheet1.Columns(1)) - 3
TotalcolCopy=oXls.Application.WorksheetFunction.Match("ABC", objSheet1.Rows(3), 0)

objSheet1.Range(objSheet1.Cells(4,1),objSheet1.Cells(TotalRows,TotalcolCopy)).Copy(objSheet2.Range("A1"))
objSheet2.Range(objSheet2.Cells(1,2),objSheet2.Cells(TotalRows,TotalcolCopy-1)).Delete(-4159)
'Dim aData : aData=objSheet2.Cells.SpecialCells(12)'xlCellTypeVisible
TotalRows = oXls.Application.WorksheetFunction.CountA(objSheet2.Columns(1))
Dim aData : aData = objSheet2.Range("A1:B"&TotalRows)

'MsgBox(LBound(aData, 1)&"And"&UBound(aData, 1))

   For nRow = LBound(aData, 1) To UBound(aData, 1)

     Set dicP(aData(nRow, 1)) = New cP

   Next

   For nRow = LBound(aData, 1) To UBound(aData, 1)

        If aData(nRow, 1) = aData(nRow, 2) Then
           dicP(aData(nRow, 1)).m_sRel = "Parent"
        Else
           dicP(aData(nRow, 2)).m_dicC.Add   aData(nRow, 1), 0         '(aData(nRow, 1)) = 0
        End If

   Next

    objSheet2.Cells.ClearContents'To clear all the previous contenets of the sheet#2

    nRow=1
    For Each nP In dicP.Keys()

    objSheet2.Cells(nRow,1).Value=nP
    objSheet2.Cells(nRow,2).Value=dicP(nP).m_sRel

       'WScript.Echo nP, dicP(nP).show()


    nRow=nRow+1  
    Next

错误:所需对象:行 dicP(aData(nRow,2)).m_dicC.'a [undefined]'.添加aData(nRow,1),0'(aData(nRow,1))= 0

编辑:要获取子列表,我用上面的代码编写了以下代码:

To get the child list,I wrote the below code with the above:

       For Each nP In dicP.Keys()

    objSheet2.Cells(nRow,1).Value=nP
    objSheet2.Cells(nRow,2).Value=dicP(nP).m_sRel
    objSheet2.Range("C"&nRow).Value=dicP(nP).m_dicC.Keys

       'WScript.Echo nP, dicP(nP).show()


    nRow=nRow+1  
       Next

但是期望的输出没有到来,您能在这里建议吗?

But Desired output is not coming,Can you suggest here?

推荐答案

我不怪您找不到问题.您的代码很难阅读,调试或弄清楚您打算做什么.

I don't blame you for not being able to find the problem. Your code is difficult to read, debug, or figure out what you're intending to do.

话虽这么说,问题是尚未定义 dicP(aData(nRow,2)),因此您无法访问 m_dicC 属性,因为它返回了一个空变体而不是对象.当您尝试调用该属性时,您会收到指示的错误消息.

That being said, the problem is that dicP(aData(nRow,2)) has not been defined and therefore you cannot access the m_dicC property because it returns an empty variant instead of an object. When you attempt to call the property, you receive the error message that you indicated.

您还可以使用更简单的脚本来复制错误:

You can also replicate the error with a much simpler script:

Dim dicP: Set dicP = CreateObject("Scripting.Dictionary")
dicP("test").JumpUpJumpUpAndGetDown 'Silly I know.

由于没有对应的对象,因此代码不知道与我传递的方法调用有什么关系,并指示需要一个对象.

Since there's no corresponding object, the code has no idea what to do with the method call I passed and indicates that an Object is Required.

我的猜测是您打算这样做:

My guess is that you either intended to do this:

 For nRow = LBound(aData, 1) To UBound(aData, 1)
     Set dicP(aData(nRow, 1)) = New cP
     'Also add a cP for the nRow, 2
     Set dicP(aData(nRow, 2)) = New cP
   Next

或者这个:

dicP(aData(nRow,1)).m_dicC.添加aData(nRow,1),0 注意(nrow,1)而不是(nrow,2)

我建议至少在代码中添加注释,因为即使您是将来维护它的人,它也似乎过于复杂并且很难掌握其功能.您可能会忘记它的工作原理,并且需要花一些时间(像我一样)弄清楚发生了什么.

I would suggest at least adding comments to your code because even if you're the one maintaining it in the future, it seems overly complex and is hard to grasp what it is doing. You'll probably forget how it works and will need to spend awhile (like I did) figuring out what is going on.

这篇关于错误:所需对象:字典对象为'[undefined]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆