VBA循环 - 使用列中的值

时间:2014-06-20 01:48:09

标签: excel vba loops excel-vba finance

Sub Combined()

Dim stockcode As String
Dim marketcode As String
stockcode = Sheets("NYSE screener").Range("B1").Value
marketcode = Sheets("Stock input").Range("B2").Value

Sheets.Add.Name = stockcode & "BS"

With Worksheets(stockcode & "BS").QueryTables.Add(Connection:= _
    "URL;http://markets.ft.com/research/Markets/Tearsheets/Financials?s=" & stockcode & ":" & marketcode & "&subview=BalanceSheet", Destination:=Range( _
    "$A$1"))
    '.Name = "67083361_zpid"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

Worksheets(stockcode & "BS").Range("A115:F153").Select
Selection.Copy
Worksheets(stockcode & "BS").Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(stockcode & "BS").Rows("40:170").Clear



Sheets.Add.Name = stockcode & "CF"

With Worksheets(stockcode & "CF").QueryTables.Add(Connection:= _
    "URL;http://markets.ft.com/research/Markets/Tearsheets/Financials?s=" & stockcode & ":" & marketcode & "&subview=CashFlow", Destination:=Range( _
    "$A$1"))
    '.Name = "67083361_zpid"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

Worksheets(stockcode & "CF").Range("A115:F142").Select
Selection.Copy
Worksheets(stockcode & "CF").Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(stockcode & "CF").Rows("29:159").Clear


Sheets.Add.Name = stockcode & "IS"

With Worksheets(stockcode & "IS").QueryTables.Add(Connection:= _
    "URL;http://markets.ft.com/research/Markets/Tearsheets/Financials?s=" & stockcode & ":" & marketcode & "&subview=IncomeStatement", Destination:=Range( _
    "$A$1"))
    '.Name = "67083361_zpid"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

Worksheets(stockcode & "IS").Range("A115:F161").Select
Selection.Copy
Worksheets(stockcode & "IS").Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(stockcode & "IS").Rows("48:178").Clear

End Sub

'我在另一张名为NYSE screener的工作表中有一列股票代码。我想使用这些值并运行整个代码来获得三个单独的工作表。我如何创建循环?这些值位于纽约证券交易所筛选器的B栏

1 个答案:

答案 0 :(得分:0)

我不确定您正在寻找什么样的答案,但是要访问外部工作簿:

Dim wbk as Workbook
Set wbk = Workbooks(<filepath>)

那么你只需像VBA那样定期引用

wbk.worksheets("Sheet1").Range("A1")

for循环的语法可能类似于:

For i in wbk.worksheets("Sheet1").Range("B2:B1000")

Next i

我建议您将代码库列表读入数组中作为您在代码中执行的第一件事(使用RDim,因此如果您不确定您拥有多少股票代码,则可以动态调整其大小)。然后,您可以遍历每个单独工作表中的数组,而无需每次都再次访问该文件。

我是从内存中写的,所以你可能需要稍微调整一下语法。