Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 用循环搜索文件管理系统中的对象_Excel_Vba - Fatal编程技术网

Excel 用循环搜索文件管理系统中的对象

Excel 用循环搜索文件管理系统中的对象,excel,vba,Excel,Vba,有人能解释为什么这个代码不正确吗?如果我分别使用每个单元格运行它,它工作得很好。然而,当我在循环中运行它时,它对第一个对象有效,但随后它继续给出0作为结果 我的代码正在文件管理系统中搜索对象 因此,如果我删除循环功能,请使用ThisWorkbook.Sheets(“Sheet”).Range(“B3”).value或ThisWorkbook.Sheets(“Sheet”).Range(“B4”).value或ThisWorkbook.Sheets(“Sheet”).Range(“B5”).val

有人能解释为什么这个代码不正确吗?如果我分别使用每个单元格运行它,它工作得很好。然而,当我在循环中运行它时,它对第一个对象有效,但随后它继续给出0作为结果

我的代码正在文件管理系统中搜索对象

因此,如果我删除循环功能,请使用
ThisWorkbook.Sheets(“Sheet”).Range(“B3”).value
ThisWorkbook.Sheets(“Sheet”).Range(“B4”).value
ThisWorkbook.Sheets(“Sheet”).Range(“B5”).value添加单元格值,而不是
,并在每次正常工作时单击按钮运行代码(每次单击时,我都会收到一条消息“有1个对象”。当我按原样运行时,我会得到:“有1个对象”,然后是“有0个对象”,“有0个对象”

这是我的密码:

'set range
Dim myrng: Set myrng = ThisWorkbook.Sheets("Sheet").Range("B3:B5")

Dim cel
'loop through each cell in range
For Each cel In myrng

        ' ObjectType
        oExp.DataStatusValueType = MFilesAPI.MFStatusTypeObjectTypeID
        oTV.SetValue MFDatatypeLookup, 305
        oSearchCondition.Set oExp, MFConditionTypeEqual, oTV
        oSearchConditions.Add -1, oSearchCondition

        ' Class
        Set oSearchCondition = CreateObject("MFilesAPI.SearchCondition")
        oExp.DataPropertyValuePropertyDef = MFilesAPI.MFBuiltInPropertyDefClass
        oTV.SetValue MFDatatypeLookup, 177
        oSearchCondition.Set oExp, MFConditionTypeEqual, oTV
        oSearchConditions.Add -1, oSearchCondition

        ' Object has the same name
        Set oSearchCondition = CreateObject("MFilesAPI.SearchCondition")
        oExp.DataPropertyValuePropertyDef = MFilesAPI.MFBuiltInPropertyDefNameOrTitle
        oTV.SetValue MFDatatypetext, cel.Value ' Here it is
        oSearchCondition.Set oExp, MFConditionTypeEqual, oTV
        oSearchConditions.Add -1, oSearchCondition

        ' Object is not deleted
        Set oSearchCondition = CreateObject("MFilesAPI.SearchCondition")
        oExp.DataStatusValueType = MFilesAPI.MFStatusType.MFStatusTypeDeleted
        oTV.SetValue MFDatatypeBoolean, False
        oSearchCondition.Set oExp, MFConditionTypeEqual, oTV
        oSearchConditions.Add -1, oSearchCondition

        ' Execute the search
        Dim oObjectSearchResults As MFilesAPI.ObjectSearchResults
        Set oObjectSearchResults = oVault.ObjectSearchOperations.SearchForObjectsByConditions(oSearchConditions, MFSearchFlagNone, False)

        MsgBox ("There were " & oObjectSearchResults.Count & " objects")

Next

好的,在PeterT的提示下,我通过在代码末尾添加以下内容,成功地使其工作:

        Set oObjectSearchResults = Nothing
        Set oSearchConditions = Nothing
        Set oSearchCondition = Nothing
        Set oExp = Nothing
        Set oTV = Nothing

这可能不是问题所在,我从未使用过M文件,但您多次重新创建
oSearchCondition
对象,而您可能只需要执行一次。在重用它时,您可能需要重置一些参数,但我认为不必继续重新创建它。在进入应用程序之前,先创建一次循环,然后更改参数。事实上,看起来您正在尝试为搜索分层多个条件。每次
CreateObject(“MFilesAPI.SearchCondition”)
您都有效地清除了之前的所有内容,并从重置对象开始。感谢您的提示!