user2743606
user2743606

Reputation: 11

Visual Basic Loop

I want the loop to query the ticker in the cell right below it and loop until it has pulled that data for all of tickers in the column.

Summary:

I am attempting to pull data from for ticker symbols in Column A This is the code I am using.

Sub URL_Static_Query()


''Pull Data from Profile

With Sheet2.QueryTables.Add(Connection:= _
      "URL;http://finance.yahoo.com/q/pm?s=" & Sheet1.Range("A2").Value & "+Performance", _
         Destination:=Sheet2.Range("A1"))
 .BackgroundQuery = True
 .TablesOnlyFromHTML = True
 .Refresh BackgroundQuery:=False
 .SaveData = True

 End With

''Pull Data from Performance
 With Sheet3.QueryTables.Add(Connection:= _
      "URL;http://finance.yahoo.com/q/pr?s=" & Sheet1.Range("A2").Value & "+Profile", _
         Destination:=Sheet3.Range("A1"))

.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With

'Grab and Paste 3-month
Sheets("Sheet2").Select
Range("A1").Select

Cells.Find(What:="3-month", After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

ActiveCell.Offset(0, 1).Select
Selection.Copy

Sheets("Sheet1").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste

'Grab and Paste 1-Year
Sheets("Sheet2").Select
Range("A1").Select

Cells.Find(What:="1-Year", After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Sheets("Sheet1").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 2).Select

Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True

 Sheets("Sheet3").Select
 Range("A1").Select

 Cells.Find(What:="Prospectus Net Expense Ratio:", After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Sheet1").Select
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste

Sheet2.Cells.Clear
Sheet3.Cells.Clear

End Sub

Upvotes: 1

Views: 373

Answers (1)

Aaron Thomas
Aaron Thomas

Reputation: 5281

You can wrap this code in a loop that goes down each cell in the column, one by one.

For example, if you're using column A,

Dim row_counter As Long, last_row As Long
row_counter = 1
'last_row = whatever your last row is

Do While row_counter < last_row
  '... put looping code here
  row_counter = row_counter + 1
Loop

Upvotes: 1

Related Questions