Reputation: 45
I am pulling data from a SQL table to a worksheet called "Results". The output gets pulled from a SQL table based on the specific date range given by the user in the main worksheet.
I am unable to delete the old records. Currently I manually delete the worksheet and create a new sheet "Results" and then run the macro. I am looking for an automated process where the results update every time a macro is called. It should replace the old data and show the results only based on the current filters.
'Open Recordset'
Set objMyRecordset.Source = objMyCmd
objMyRecordset.Open
For iCols = 0 To objMyRecordset.Fields.Count - 1
Worksheets("Results").Cells(1, iCols + 1).Value = objMyRecordset.Fields(iCols).Name
Next
'Copy Data to Excel'
Worksheets("Results").Range("A2").CopyFromRecordset objMyRecordset
Upvotes: 0
Views: 1175
Reputation: 166511
Something like
Worksheets("Results").Range("A1").CurrentRegion.Clearcontents
before adding the new data should do it
Upvotes: 1