HI
I have a macro that suppose to select data from a named range in excel and transfer it to the newly created sheet in another spreadsheet using ADo recordset.
It seems like there is a bug in this piece of code :
sSql = "SELECT * INTO ['" & sExportToFile & "'].['" & sNewSheetName & "'] FROM MyTable" 'sDataBase
rsExcel.Open sSql, sConnect, adOpenDynamic, adLockOptimistic
I get "Invalid argument" error here.
When i tried to replace MyTable with the name of an active worksheet : 'sDataBase = "[" & ActiveSheet.Name & "$]"
I got an error saying that Microsoft jet could not find the sheet object.
I am sure about all the spellings, all files exist, paths are correct.
Can anyone spot the bug in my code?
It might be something obvious but i just can't see it right now.
Thank you
here's my code :
Sub adoExcelEXPORT() 'USING RECORDSET
Dim rsExcel As ADODB.Recordset
'Dim sDataBase As String
Dim sExportToFile As String
Dim sNewSheetName As String
Dim sConnect As String
Dim sSql As String
Dim iLastRow As Integer
'CREATE A TABLE TO TRANSFER
iLastRow = Cells(Rows.Count, "a").End(xlUp).Row
Range("a5:c" & iLastRow).Name = "MyTable"
'sDataBase = "[" & "MyTable" & "]" 'if transferring one table
'sDataBase = "[" & ActiveSheet.Name & "$]" 'if transferring the whole sheet
'CREATE PATH FOR A DESTINATION FILE AND NAME FOR A NEW SHEET
sExportToFile = Range("ExportPath")
sNewSheetName = Format(Date, "ddmmyy") & "Download"
'ADO ROUTINES :
Set rsExcel = New ADODB.Recordset
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDataBase & _
";Extended Properties=Excel 8.0;"
'SQL COMMAND - Transfer data from 'MyTable' to newly created Sheet(sNewSheetName) in sExportToFile
sSql = "SELECT * INTO ['" & sExportToFile & "'].['" & sNewSheetName & "'] FROM MyTable" ''''''& sDataBase
'EXECUTION
rsExcel.Open sSql, sConnect, adOpenDynamic, adLockOptimistic
'Clean Up and free memory
ActiveWorkbook.Names("MyTable").Delete
Set rsExcel = Nothing
End Sub
I have a macro that suppose to select data from a named range in excel and transfer it to the newly created sheet in another spreadsheet using ADo recordset.
It seems like there is a bug in this piece of code :
sSql = "SELECT * INTO ['" & sExportToFile & "'].['" & sNewSheetName & "'] FROM MyTable" 'sDataBase
rsExcel.Open sSql, sConnect, adOpenDynamic, adLockOptimistic
I get "Invalid argument" error here.
When i tried to replace MyTable with the name of an active worksheet : 'sDataBase = "[" & ActiveSheet.Name & "$]"
I got an error saying that Microsoft jet could not find the sheet object.
I am sure about all the spellings, all files exist, paths are correct.
Can anyone spot the bug in my code?
It might be something obvious but i just can't see it right now.
Thank you
here's my code :
Sub adoExcelEXPORT() 'USING RECORDSET
Dim rsExcel As ADODB.Recordset
'Dim sDataBase As String
Dim sExportToFile As String
Dim sNewSheetName As String
Dim sConnect As String
Dim sSql As String
Dim iLastRow As Integer
'CREATE A TABLE TO TRANSFER
iLastRow = Cells(Rows.Count, "a").End(xlUp).Row
Range("a5:c" & iLastRow).Name = "MyTable"
'sDataBase = "[" & "MyTable" & "]" 'if transferring one table
'sDataBase = "[" & ActiveSheet.Name & "$]" 'if transferring the whole sheet
'CREATE PATH FOR A DESTINATION FILE AND NAME FOR A NEW SHEET
sExportToFile = Range("ExportPath")
sNewSheetName = Format(Date, "ddmmyy") & "Download"
'ADO ROUTINES :
Set rsExcel = New ADODB.Recordset
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDataBase & _
";Extended Properties=Excel 8.0;"
'SQL COMMAND - Transfer data from 'MyTable' to newly created Sheet(sNewSheetName) in sExportToFile
sSql = "SELECT * INTO ['" & sExportToFile & "'].['" & sNewSheetName & "'] FROM MyTable" ''''''& sDataBase
'EXECUTION
rsExcel.Open sSql, sConnect, adOpenDynamic, adLockOptimistic
'Clean Up and free memory
ActiveWorkbook.Names("MyTable").Delete
Set rsExcel = Nothing
End Sub