2017-04-13 67 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