Hello Yasser,
Could you please help me with this code:
This code pastes data form an open workbook to a closed workbook successfully for only two columns (there could be many rows), But I need to add data in closed worbook for many coulmns not limited to 2 columns.
Please suggest.! Thanks
Sub ExportToClosedWorkbookUsingADO()
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset, strFile As String
Dim i As Long, j As Long
strFile = ThisWorkbook.Path & "\Sample.xlsm"
#If VBA7 And Win64 Then
Set conn = CreateObject("Adodb.Connection")
conn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & strFile & ";" & "Extended Properties=""Excel 12.0;HDR=Yes"""
#Else
Set conn = CreateObject("ADODB.Connection")
conn.Open "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & strFile & ";HDR=Yes';"
#End If
Set rs = CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM [Master File$]", conn, 1, 3
For j = 2 To Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
rs.AddNew
For i = 0 To Sheets("Data").Cells(1, Columns.Count).End(xlToLeft).Column - 1
rs(i).Value = Sheets("Data").Cells(j, i + 1).Value
Next i
rs.Update
Next j
conn.Close
Set rs = Nothing
Set conn = Nothing
MsgBox "Updated...", vbInformation
End Sub