My main objective is to transfer some data from excel to SQL server while using vba but in the process I would like to detect and avoid transferring some duplicate month columns.
This is what i have so far:
Sub UploadExcelToSQL() Dim adoCN As ADODB.Connection Dim sConnString As String Dim sSQL As String Dim lRow As Long, lCol As Long sConnString = "Provider=SQLOLEDB;Data Source=agrsql004\instance01;Initial Catalog=DEHL;Integrated Security=SSPI" Set adoCN = CreateObject("ADODB.Connection") adoCN.Open sConnString For lRow = 2 To 4 sSQL = "INSERT INTO dbo.MonthlyValue (ID, Year, Month, Value) " & _ " VALUES (" & _ "'" & Sheet2.Cells(lRow, 1) & "', " & _ "'" & Sheet2.Cells(lRow, 2) & "', " & _ "'" & Sheet2.Cells(lRow, 3) & "', " & _ "'" & Sheet2.Cells(lRow, 4) & "')" adoCN.Execute sSQL Next lRow adoCN.Close Set adoCN = Nothing End Sub And will return something like:
ID Year Month Value 123 2018 1 9987 123 2018 2 80988 123 2018 1 8990 What I would like is to have something like this:
ID Year Month Value 123 2018 1 9987 123 2018 2 80988 In conclusion, I would like the code to detect the duplicate months of an equal year and avoid transferring it to the DB in SQL server so that one ID can only have one value per month of x year. My knowledge is pretty limited in this area still so any help would be greatly appreciated.