I have macro that runs a little check and extracts data which is then transferred to another excel file (Price check.xls).
Before the transfer happens I wanted it to clear all the records from the previous run.
Initially I tried "DELETE... WHERE.. Price_date<> date" syntax but it seems like it doesn't work in excel as it does in Access and you can't delete the whole rows.
Instead i decided to delete and recreate the table where my extracted records would be appended.
The problem is that when i delete the old table with say 50 records, the new table will appear starting from the row 51 which is a bit confusing for the end user.
Can anyone think of a way of making the new data set to appear starting from the first cell?
thank you
here's my code (should work on any pc with excel 2003 after replacing a path in sDataDropFile = )
'----------------------------------------------------------
Public conExcel As New ADODB.Connection
Public sSql As String
Public sDataDropFile As String
Sub MainMacro()
Call ConnectToExcelFIle
Call DeleteOldTable
Call CreateNewTable
Call InsertData
End Sub
'----------------------------------------------------------
Private Sub ConnectToExcelFIle()
' Connect to the spreadsheet
sDataDropFile = "R:\lifeaccts\INVESTME\New Hierarchy Structure\Income\Valuations\2% CHECKS\Price check.xls"
conExcel.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sSourceFile & _
";Extended Properties=Excel 8.0;"
End Sub
'-----------------------------------------------------------
Private Sub DeleteOldTable()
On Error Resume Next
sSql = "DROP TABLE [Sheet1$]"
conExcel.Execute sSql
On Error GoTo 0
End Sub
'-----------------------------------------------------------
Private Sub CreateNewTable()
sSql = ""
sSql = "CREATE TABLE [Sheet1$](" & _
"Sedol string(7)," & _
"Security string," & _
"Offer_price LONG," & _
"Price_Mvmnt LONG," & _
"Price_date DATETIME)"
conExcel.Execute sSql
End Sub
'-----------------------------------------------------------
Private Sub InsertData()
'Insert New Data
For i = 1 To 5
' dummy data :
sSedol = "b" & i
sName = "Vodafone"
dPrice = 10.24
dtedate = Format(Date, "mm/dd/yy")
sSql = ""
sSql = "Insert into [[Sheet1$]] (Sedol, Security,Offer_price,Price_Mvmnt,Price_date)" & _
" values ('" & sSedol & "','" & sName & "'," & dPrice & ",4, #" & dtedate & "#)"
conExcel.Execute sSql
Next
conExcel.Close
Set conExcel = Nothing
End Sub