Sub SendRangeToSharePointList()
Const SharePointURL As String = "https://your-sharepoint-site-url"
Const ListName As String = "Gredang SharePoint List"
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:D10")
Dim xmlHttp As Object
Dim strBatchXML As String
Dim i As Long
Dim xmlBatchItem As String
Dim url As String
On Error Resume Next
Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
If xmlHttp Is Nothing Then
MsgBox "Microsoft XML, v6.0 is required for this code to run. Go to Tools > References and check the box for 'Microsoft XML, v6.0'.", vbExclamation
Exit Sub
End If
On Error GoTo ErrorHandler
strBatchXML = "<?xml version=""1.0"" encoding=""UTF-8""?><Batch>"
For i = 2 To rng.Rows.Count
xmlBatchItem = "<Method ID=""" & i & """ Cmd=""New"">"
xmlBatchItem = xmlBatchItem & "<Field Name='Title'>" & rng.Cells(i, 1).Value & "</Field>"
xmlBatchItem = xmlBatchItem & "<Field Name='Column1'>" & rng.Cells(i, 2).Value & "</Field>"
xmlBatchItem = xmlBatchItem & "<Field Name='Column2'>" & rng.Cells(i, 3).Value & "</Field>"
xmlBatchItem = xmlBatchItem & "</Method>"
strBatchXML = strBatchXML & xmlBatchItem
Next i
strBatchXML = strBatchXML & "</Batch>"
url = SharePointURL & "/_vti_bin/listdata.svc/" & ListName
xmlHttp.Open "POST", url, False
xmlHttp.setRequestHeader "Content-Type", "application/xml;charset=utf-8"
xmlHttp.setRequestHeader "X-HTTP-Method", "POST"
xmlHttp.setRequestHeader "Accept", "application/atom+xml"
xmlHttp.setRequestHeader "X-RequestDigest", GetRequestDigest(SharePointURL)
xmlHttp.send strBatchXML
If xmlHttp.Status = 201 Then
MsgBox "Data successfully sent to SharePoint list.", vbInformation
Else
MsgBox "An error occurred while sending data to SharePoint list. Status: " & xmlHttp.Status & " - " & xmlHttp.statusText, vbExclamation
End If
Exit Sub
ErrorHandler:
MsgBox "An error occurred. Error: " & Err.Description, vbExclamation
End Sub
Function GetRequestDigest(ByVal url As String) As String
Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
xmlHttp.Open "POST", url & "/_api/contextinfo", False
xmlHttp.setRequestHeader "Accept", "application/json;odata=verbose"
xmlHttp.send ""
Dim jsonResponse As Object
Set jsonResponse = JsonConverter.ParseJson(xmlHttp.responseText)
GetRequestDigest = jsonResponse("d")("GetContextWebInformation")("FormDigestValue")
End Function