PDA

View Full Version : Macro to copy excel recordset and insert in to Database based on a condition



chakkrav
08-06-2017, 12:43 PM
In Column "U" i have values "Y" or "N"
E.g. If U2 cell value is "Y" it should copy the whole row range A2:T2 recordset and insert the values in to a table in database &
should loop it till the end of the data. I would greatly Appreciate and welcome VBA experts advise in this regards.

mana
08-07-2017, 03:09 AM
Option Explicit


Sub test()
Dim wsS As Worksheet
Dim wsD As Worksheet

Set wsS = Sheets("Source")
Set wsD = Sheets("Databese")

With wsS.Range("U1", wsS.Range("U" & Rows.Count).End(xlUp))
.AutoFilter
.AutoFilter 1, "Y"
If .SpecialCells(xlCellTypeVisible).Count > 1 Then
.Offset(1).EntireRow.Columns("A:T").Copy _
wsD.Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
.AutoFilter
End With

wsD.Activate

End Sub

chakkrav
08-07-2017, 04:31 AM
Hi Mana,

Many thanks for the reply.
But database is not a worksheet. I meant copy the source worksheet data to oracle database table. How should i use insert into SQL query in the code to copy the above filtered data.
Thanks in Advance

mana
08-07-2017, 05:01 AM
Sorry. Please wait for the answers of other respondents.