I am trying to compare one column of an excel sheet with the database table.If the value in the column matches with the data in the table,the excel has to be populated with another value in the table in a new column.If match is not found the value in the new column should be kept as "X".

Dim i As Integer
i = 1
datafile = "database.accdb"
With cn
    .Provider = "microsoft.ACE.OLEDB.12.0"
    .ConnectionString = datafile
End With

Dim oXL As Object
Set oXL = CreateObject("Excel.Application")
Dim oBook As Object

Set oBook = oXL.Workbooks
Dim oSheet As Object
'Set oSheet = oXL.Worksheets(1)
Dim vValue As Variant    

Set oBook = oXL.Workbooks.Open("C:\s.xlsx")
Set oSheet = oBook.Worksheets("Sheet1")

Do Until (i >= 5)

    vValue = oSheet.Cells(i, 5).Value
    MsgBox vValue

    Set rs = cn.Execute("select * from table where ID = vValue ")

    If (rs.EOF = False) Then
        oSheet.Cells(i, 5).Value = rs!field
        oSheet.Cells(i, 5).Value = "X"
    End If


    i = i + 1

This article has been dead for over six months. Start a new discussion instead.