0

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
Sebastian
  • 13
  • 2

3 Answers3

1

Add the following under the Sub finstate() :

Worksheets("your sheet name").Range("B1:G50000").Clear
Ionut
  • 91
  • 1
  • 10
0

Seeing that you use Activesheet, clearing like this is also an option:

ActiveSheet.Columns("B:G").Clear

Vityata
  • 42,633
  • 8
  • 55
  • 100
0

I think this is a lot better, especially for importing data for several tickers.

Sub Macro1()

ThisSheet = ActiveSheet.Name
Range("A2").Select
Do Until ActiveCell.Value = ""
Symbol = ActiveCell.Value
Sheets(ThisSheet).Select
Sheets.Add


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

Sheets(ActiveSheet.Name).Name = Symbol
Sheets(ThisSheet).Select
ActiveCell.Offset(1, 0).Select

Loop

End Sub

My Sheet1 looks like this:

enter image description here

When the script finishes running, you will have something like this:

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200