0

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
Community
  • 1
  • 1
Shasti
  • 45
  • 9

1 Answers1

1

Something like

 Worksheets("Results").Range("A1").CurrentRegion.Clearcontents

before adding the new data should do it

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Sorry I missed the part about needing to preserve the filters. See here for code to do that: http://stackoverflow.com/questions/9489126/in-excel-vba-how-do-i-save-restore-a-user-defined-filter – Tim Williams Dec 28 '16 at 18:40