Search code examples
excelvbastatementsfinancialstocks

Financial Statements pasting next to old one


So here I have a vba code that populates financial statements for different companies, when I first run the macro it pastes the information from columns B to G, yet when I re-run it, it will paste to the right of the old data in columns H to M and not delete the old data. I want it to delete the old data so that it pastes the new info in columns B to G, overwriting the old data every time I run the macro.

Below is my code

Thanks a million!

    Sub finstate()

      sTicker = Range("A1").Value
      If sTicker = "" Then
MsgBox "No value to look up"
Exit Sub
      End If

      With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.advfn.com/stock-market/NASDAQ/" & sTicker & "/financials?btn=annual_reports&mode=company_data" _
, Destination:=Range("B2"))
.Name = "financials?btn=annual_reports&mode=company_data"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingAll
.WebTables = "6"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
    End With
    End Sub

Solution

  • Add the following under the Sub finstate() :

    Worksheets("your sheet name").Range("B1:G50000").Clear