Class ModelRelationships (Excel VBA)

This collection contains all relationships between data tables in the data model of Excel 2013. To use a ModelRelationships class variable it first needs to be instantiated, for example


Dim mrs as ModelRelationships
Set mrs = ActiveWorkbook.Model.ModelRelationships

For Each

Here is an example of processing the ModelRelationships items in a collection.


Dim mrel As ModelRelationship
For Each mrel In ActiveWorkbook.Model.ModelRelationships
	
Next mrel

Add

Adds a new relationship to the model.

Add (ForeignKeyColumn, PrimaryKeyColumn)


Dim mrel As ModelRelationship
Set mrel = ActiveWorkbook.Model.ModelRelationships.Add(ForeignKeyColumn:=, PrimaryKeyColumn:=)

Arguments

The following arguments are required:

ForeignKeyColumn (ModelTableColumn) - A ModelTableColumn object representing the foreign key column in the table on the many side of the one-to-many relationship.

PrimaryKeyColumn (ModelTableColumn) - A ModelTableColumn object representing the primary key column in the table on the one side of the one-to-many relationship.

Count

Returns a Long value that represents the number of ModelRelationship objects in a ModelRelationships object.


Dim lngCount As Long
lngCount = ActiveWorkbook.Model.ModelRelationships.Count

DetectRelationships

Detects model relationships in the specified PivotTable object.

DetectRelationships (PivotTable)

PivotTable: The PivotTable in which to detect model relationships.


ActiveWorkbook.Model.ModelRelationships.DetectRelationships PivotTable:=

Item

Returns a single object from the ModelRelationships object.

Item (Index)

Index: The index number for the object.


Dim mrel As ModelRelationship
Set mrel = ActiveWorkbook.Model.ModelRelationships(Index:=1)