Insert data from a web page in Excel - VBA

I need to insert data from an xls file from the Internet into the current open workbook. The problem is that if I run this macro several times, it always creates a new sheet. Ideally, I would like to delete the old content and replace it with new content on the same sheet. Maybe? Here is my code

Sub downloadData()

Dim wkbMyWorkbook As Workbook
Dim wkbWebWorkbook As Workbook
Dim wksWebWorkSheet As Worksheet
Dim currentDate As Date

Set wkbMyWorkbook = ActiveWorkbook

currentDate = Date

Workbooks.Open ("https://www.somewebpage.com/file.xls")

Set wkbWebWorkbook = ActiveWorkbook
Set wksWebWorkSheet = ActiveSheet

wksWebWorkSheet.Copy After:=wkbMyWorkbook.Sheets(Sheets.Count)

wkbMyWorkbook.Activate
wkbWebWorkbook.Close

End Sub
+4
source share
1 answer

Assuming the sheet discarded from the Internet will always be the last sheet in the book, use this line:

Application.DisplayAlerts = False
wkbMyWorkbook.Worksheets(wkbMyWorkbook.Worksheets.Count).Delete
Application.DisplayAlerts = True

before copying a new sheet.

+2
source

Source: https://habr.com/ru/post/1686145/


All Articles