2017-04-13 35 views
0

因此,在这里我有一个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 

回答

1

添加下Sub finstate()如下:

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

这个工作。非常感谢! – Sebastian

+0

你可以标记为完成,thx! – Ionut

0

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

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

0

我觉得这是一个很大更好,特别是用于导入多个代号的数据。

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 

我的工作表Sheet1看起来是这样的:

enter image description here

当脚本运行完毕,就会有这样的事情:

enter image description here