Code Improvement - SQL Statement Update SharePoint List

TEIXEIRE

New Member
Joined
Apr 17, 2020
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hello experts,

I have the code below which updates some columns of SharePoint list, based on excel values, through its ID in a loop, however if I have hundreds of entries it take some time intil updates, so I would like to know with you can help with improvements in the code. It uses SQL statements in VBA to update the SharePoint List. The Activex must be enabled.

Many thanks !

VBA Code:
Option Explicit
Sub Update()

Dim tbl As ListObject
Dim x As Long

Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim mySQL As String

'TABLE Name
Set tbl = ActiveSheet.ListObjects("RSN_Technology_Change")

    'From first row of TABLE body/content until its last
    For x = 1 To tbl.ListRows.Count
    
        'additional/rule column to avoid loop
        If tbl.DataBodyRange(x, 10) = "" Then
        
            Set cnt = New ADODB.Connection
            Set rst = New ADODB.Recordset

            mySQL = "SELECT * FROM [RSN Technology Change] Where [ID] = " & tbl.DataBodyRange(x, 1) & ";"
            
            With cnt                                                                        
                .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=https://companyServer.sharepoint.com/teams/AIBProduct;LIST={dc3d5a3c-762f-4211-9c66-1c6bde4c2f71};"
                .Open
            End With
            
            rst.Open mySQL, cnt, adOpenDynamic, adLockOptimistic
            
            If Not (rst.BOF And rst.EOF) Then
                rst!Status = tbl.DataBodyRange(x, 8)
                'Field Name with space put between []
                rst![Reason for Rejection] = tbl.DataBodyRange(x, 9)
                rst![Done Date] = Format(Now(), "dd/mm/yyyy HH:mm")
                rst.Update
            End If
            
            'additional/rule column to avoid loop
            tbl.DataBodyRange(x, 10) = "OK"
        
        Else
        End If
        
    Next x

'Close SQL Connection
If CBool(rst.State And adStateOpen) = True Then rst.Close
Set rst = Nothing
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing

tbl.Refresh

MsgBox "your code has been executed", vbInformation, "Strategic Material Master Data"

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

John_w can you help ?

IDRSNSBUL1L1 CodeL2L2 CodeStatusReason For Rejection
1530581743AACThermal Management (TM)110TM_Treated Powders293Approved
1532582492AACThermal Management (TM)110TM_Treated Powders293Approved
1541590188AACThermal Management (TM)110TM_Liquids Low Viscosity287Approved
1542640384AACThermal Management (TM)110TM_Liquids Standard Viscosity286Approved
1545640381AACThermal Management (TM)110TM_Liquids Standard Viscosity286Approved
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,552
Members
453,052
Latest member
ezzat

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