2014-06-20 4 views
0

하위 결합()VBA 루핑 - 열에서 값을 사용하여

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 

최종 하위

'나는 뉴욕 증권 거래소의 스크리너라는 또 다른 워크 시트의 주식 코드의 열을 가지고있다. 값을 사용하고 전체 코드를 실행하여 세 개의 개별 워크 시트를 얻고 싶습니다. 어떻게 루프를 만들 수 있습니까? 값은 뉴욕 증권 거래소의 스크리너의 열 B에

답변

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을 사용하면 얼마나 많은 주식 코드가 있는지 확실하지 않은 경우 동적으로 크기를 조정할 수 있습니다). 매번 파일을 다시 액세스 할 필요없이 각 시트의 배열을 반복 할 수 있습니다.

메모리에서 작성 중이므로 구문을 약간 조정해야 할 수도 있습니다.

+0

안녕하세요, Isaac, 우선 재고 목록을 배열로 정의한 다음 재고 목록의 각 항목에 대해 전체 매크로를 수행하도록 정의해야합니다. 배열을 검색 할 위치를 어떻게 정의합니까? – user3758532

관련 문제