View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Matthew Herbert[_3_] Matthew Herbert[_3_] is offline
external usenet poster
 
Posts: 149
Default Importing from Web

Doug,

It's a bit hard to tell how to provide the best help without some code or
data examples. Putting the examples aside, I quickly threw together an
example of XMLHTTP below, which queries Yahoo's site for stock pricing data.
(Be sure to test the code, which returns daily pricing into a string
variable). The code uses late binding (i.e. the CreateObject line) as
opposed to adding the XML reference (Tools | Reference). This is one example
of how to get internet data without using the built-in web query. Simply run
TestXMLHTTP.

As for the "column header" portion, I'm not sure that I completely follow
your explanation. Without seeing the data, my best guess would be for you to
look into the VLOOKUP, HLOOKUP, MATCH, and/or INDEX functions to perform the
table match.

Best,

Matthew Herbert

Sub TestXMLHTTP()

Dim dteStart As Date
Dim dteEnd As Date
Dim strRes As String

dteStart = DateSerial(2007, 1, 1)
dteEnd = Date
strRes = GetYahooXMLHTTP("APOL", dteStart, dteEnd)
If strRes = "" Then
MsgBox "Data not downloaded."
Else
MsgBox "Data downloaded."
End If

End Sub

Function GetYahooXMLHTTP(strTicker As String, _
dateStart As Date, _
dateEnd As Date) As String

Dim objXMLHTTP As Object
Dim strURL As String
Dim strMonthStart As String
Dim strMonthEnd As String
Dim strDayStart As String
Dim strDayEnd As String
Dim strYearStart As String
Dim strYearEnd As String
Dim strText As String

'set up the start dates
strMonthStart = CStr(Format(DateSerial(Year(dateStart), Month(dateStart) -
1, Day(dateStart)), "mm"))
If strMonthStart = "12" Then strMonthStart = "00"
strDayStart = CStr(Format(DateSerial(Year(dateStart), Month(dateStart),
Day(dateStart)), "dd"))
strYearStart = CStr(Format(DateSerial(Year(dateStart), Month(dateStart),
Day(dateStart)), "yyyy"))

'set up the end dates
strMonthEnd = CStr(Format(DateSerial(Year(dateEnd), Month(dateEnd) - 1,
Day(dateEnd)), "mm"))
If strMonthEnd = "12" Then strMonthEnd = "00"
strDayEnd = CStr(Format(DateSerial(Year(dateEnd), Month(dateEnd),
Day(dateEnd)), "dd"))
strYearEnd = CStr(Format(DateSerial(Year(dateEnd), Month(dateEnd),
Day(dateEnd)), "yyyy"))

'Yahoo! URL
strURL = "http://ichart.finance.yahoo.com/table.csv?s=" & strTicker & _
"&a=" & strMonthStart & "&b=" & strDayStart & "&c=" & strYearStart
& _
"&d=" & strMonthEnd & "&e=" & strDayEnd & "&f=" & strYearEnd &
"&g=d" '&ignore=.csv

'create the XMLHTTP object
Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")

'query the server
With objXMLHTTP
.Open "GET", strURL, False
.send
strText = .responseText
End With

If objXMLHTTP.statusText < "OK" Then
GetYahooXMLHTTP = ""
Else
GetYahooXMLHTTP = strText
End If

End Function

"Doug" wrote:

I have two questions.
When I do a web import, I am importing a sizable amount of data. It covers
from O3:BL800. I am assuming this is a lot. It is very slow and does not
always work, so I have to click refresh a couple times in order to get it to
work. I have a fast internet connection. Sometimes I have to go into the edit
query to recheck the data that I want imported. It seems to forget. So my
first question is how can I get this to work proficiently?
Second question is. I have been importing the data and then copying and
pasting it to a table with like headers. I have been using a macro to do this
in a much shorter time. I would like to know if there is a way that this can
automatically ascribe the data to the given header column into my table at
the time it imports the data? One reason for this is because the data
imported is not in the columns I would prefer. Example. I would like to place
the data in with the same column header. Can I do this? I looked at the
properties and options for the query and it seems like I should be able to do
this. I tried finding a tutorial and help data but it was not very helpful to
me. I am using Excel 2007. Can you please tell me how to make this work
proficiently?

Thank you so much!
--
Thank you!