粘贴在旧报表旁边的财务报表

Financial Statements pasting next to old one

所以这里我有一个 vba 代码,用于填充不同公司的财务报表,当我第一次 运行 宏时,它将信息从 B 列粘贴到 G,但是当我重新 运行它,它会粘贴到H到M列旧数据的右边,不会删除旧数据。我希望它删除旧数据,以便将新信息粘贴到 B 到 G 列中,每次我 运行 宏时都会覆盖旧数据。

下面是我的代码

万分感谢!

    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

Sub finstate()下添加以下内容:

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

看你用的是Activesheet,这样清除也是一种选择:

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

我认为这要好得多,尤其是对于导入多个代码的数据。

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"))
        .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

我的 Sheet1 看起来像这样:

脚本完成后 运行,您将得到如下内容: