下面的行抛出"运行时错误5":无效的过程调用或参数

时间:2016-10-27 05:55:10

标签: excel vba

如果有人建议在excel宏中添加ModelRelationship.Add时为什么会抛出错误

那会很棒
'Add the relation
Public Sub AddRelation(ParentTableName As String, ParentColumnName As String, ChildTableName As String, ChildColumnName As String)

    Dim relations As ModelRelationships
    Set relations = ActiveWorkbook.model.ModelRelationships

    Dim tables As ModelTables
    Set tables = ActiveWorkbook.model.ModelTables

    Dim ptIndex As Integer
    ptIndex = TableIndex(ParentTableName, tables)

    Dim pfIndex As Integer
    pfIndex = ColumnIndex(ParentColumnName, tables(ptIndex))


    Dim ftIndex As Integer
    ftIndex = TableIndex(ChildTableName, tables)

    Dim ffIndex As Integer
    ffIndex = ColumnIndex(ChildColumnName, tables(ftIndex))

    **relations.Add _
        ForeignKeyColumn:= _
            tables(ftIndex).ModelTableColumns(ffIndex), _
        PrimaryKeyColumn:= _
            tables(ptIndex).ModelTableColumns(pfIndex)**

End Sub

0 个答案:

没有答案