.Model.ModelRelationships.Add loop how to speed up script

Belina65

New Member
Joined
Aug 2, 2022
Messages
1
Platform
  1. Windows
Hello,

I'm a java backend developer (spring an so on) I know nothing about visual basic scripts in Excel, at work they gave me a task to speed up this script (ROTFL),
The execution time is about 6-8 hours. I read that accessing data from a loop is a bad practice, Please help me on how to speed up this (using regions perhaps ? ), Thank you.

VBA Code:
Sub CREATE_RELATIONSHIPS(ByRef wb As Workbook)

    Dim sh As Worksheet
    Dim rw As Variant
    Dim RowCount As Integer
    Dim PK_T As String, PK_C As String, FK_T As String, FK_C As String
    Const CREATING_RELATION_CONST = "Creating Relationship "
    Dim start As Double
    Dim realationsCounter As Long
    start = Timer
    RowCount = 0
    
    Set sh = wb.Sheets("Relations")
    
    For Each rw In sh.Rows
      If sh.Cells(rw.Row, 1).Value = "" Then
        Exit For
      End If
      
      If RowCount >= 1 Then
        FK_T = sh.Cells(rw.Row, 1).Value
        FK_C = sh.Cells(rw.Row, 2).Value
        PK_T = sh.Cells(rw.Row, 3).Value
        PK_C = sh.Cells(rw.Row, 4).Value
        
        Debug.Print CREATING_RELATION_CONST, FK_T, FK_C, PK_T, PK_C
        'On Error Resume Next
        wb.Model.ModelRelationships.Add wb.Model.ModelTables(FK_T).ModelTableColumns(FK_C), wb.Model.ModelTables(PK_T).ModelTableColumns(PK_C)
        realationsCounter = realationsCounter + 1
        'If Err Then
        '    Debug.Print "Error"
        'Else
        '    Debug.Print "Created relationship"
        'End If
        
      End If
      RowCount = RowCount + 1
    Next rw
    
    Debug.Print realationsCounter, " relations created."
End Sub

thank you very very much
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top