Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples XML
 

Visual Basic Excel


Code ResourceAuthor: newton12  

Difficulty Level: Intermediate

Published: 28th Aug 2009   Read: 1291 times  

Filed in: XML
Add Comment


 


In Column B is a list of "ticker symbols." Anything can be in column A. There is a title in B1, which can be anything, I like to use the date. Code will access Yahoo finance and find the change from the previous day close for each symbol. Column headers: (A1)SignalDate (B1)8/26/2009 (C1)LastTrade (D1)PrevClose (E1)Change (F1)Vol (C1 through F1 are put in by the code, but A1 and B1 are supplied by the user and can be anything the user wants) (Column B are the symbols which will be looked up) B1 = 8/26/2009 B2 = ^GSPC B3 = CP etc. = CLNE MTW PCU TRMA CENX FOE KWR ZEUS TRW CZZ SORL OMX EK SPP OMX EK SPP SRLS CPD tpi BKD SRZ LPX FLS GTI LDSH ALU MEDQ UIS RMTR NLST CP
 


Sample Code
  1. Sub FastLookUp()
  2.   Range("G3").Select
  3.   Range("C1").Value = "LastTrade"
  4.   Range("D1").Value = "PrevClose"
  5.   Range("E1").Value = "Change"
  6.   Range("F1").Value = "Vol"
  7.   If Range("B2").Value <> "^GSPC" Then
  8.     Rows("2:2").EntireRow.Select
  9.     Selection.Insert Shift:=xlDown
  10.     Range("G3").Select
  11.     Range("B2").Value = "^GSPC"
  12.   Else
  13.   End If
  14.  
  15.   ThisurSP500 = "http://finance.yahoo.com/q?s=^GSPC"
  16.   Set QT = ActiveSheet.QueryTables.Add( _
  17.     Connection:="URL;" & ThisurSP500, _
  18.     Destination:=Range("G2"))
  19.   With QT
  20.     .WebSelectionType = xlSpecifiedTables
  21.     .WebTables = "1"
  22.     .Refresh BackgroundQuery:=False
  23.   End With
  24.   LastTrade = Range("H2").Value
  25.   PreviousClose = Range("H5").Value
  26.   Change = (LastTrade - PreviousClose) / PreviousClose
  27.   Range("C2").Value = LastTrade
  28.   Range("D2").Value = PreviousClose
  29.   Range("E2").Value = Change
  30.   Range("e2").NumberFormat = ".00%"
  31.   For Each n In ActiveSheet.Names
  32.     n.Delete
  33.   Next
  34.   For Each QT In Worksheets(ActiveSheet.Name).QueryTables
  35.     QT.Delete
  36.   Next QT
  37.   Columns("G:H").Delete
  38.   'End SP500
  39.   For Each sym In Range("b3:b" & Cells(Rows.Count, 1).End(xlUp).Row)
  40.     Thisurl = "http://finance.yahoo.com/q?s=" & sym
  41.     Set QT = ActiveSheet.QueryTables.Add( _
  42.       Connection:="URL;" & Thisurl, _
  43.       Destination:=Range("G2"))
  44.     With QT
  45.       .WebSelectionType = xlSpecifiedTables
  46.       .WebTables = """table1"",""table2"""
  47.       .Refresh BackgroundQuery:=False
  48.     End With
  49.     For Each n In ActiveSheet.Names
  50.       n.Delete
  51.     Next
  52.     For Each QT In Worksheets(ActiveSheet.Name).QueryTables
  53.       QT.Delete
  54.     Next QT
  55.     LastTrade = Range("H2").Value
  56.     sym.Offset(, (1)).Value = LastTrade
  57.     PrevClose = Range("H5").Value
  58.     sym.Offset(, 2).Value = PrevClose
  59.     TheChange = (LastTrade - PrevClose) / PrevClose
  60.     sym.Offset(, 3).Value = TheChange
  61.     sym.Offset(, 3).NumberFormat = ".00%"
  62.     Volumn = Range("H13").Value
  63.     sym.Offset(, 4).Value = Volumn
  64.     Columns("G:H").Delete
  65.   Next sym
  66. End Sub
  67.  
Copyright GeekInterview.com


Next Article: Basic XML code


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





Popular Coders

# Coder NameHits
1. vivek1634
2. newton121292
3. chowsys1241

Active Coders

# Coder NameCodes
1. chowsys1
2. vivek1
3. newton121

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape