The following code is used to delete old data and then download a text file with new data.
It does the job as far as the worksheet is concerned but I have found that every time the data is renewed I get two new ranges named with FileName plus an increasing number "Budget 2017_2018"1,2,3.....n
When you delete a Range populated by a query manually you are asked whether the query should be deleted as well. I think I need some code to do this but without asking for permission. Can anyone help please?
Sub FetchText(SheetName, FileName)
On Error Resume Next
With Sheets(SheetName)
.Range(SheetName & "_Clear").Clear
.Range(SheetName & "_Transpose").Clear
End With
On Error GoTo 0
FilePath = Sheets("Utilities").Range("J3").Value
ConnStr = "Text;" & FilePath & FileName
With Sheets(SheetName).QueryTables.Add(Connection:=ConnStr, Destination:=Range("A9"))
.Name = FileName
.FieldNames = True
.SavePassword = False
.Refresh BackgroundQuery:=False
End With
Range(SheetName & "_Table").Copy
Sheets(SheetName).Range("A28").PasteSpecial Transpose:=True
End Sub
Bookmarks