View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Loop through date range, but skip down to next year


Does the following do what you want? Note that you do not have to put your dates in Column A first... the code handles that for you.

Sub DistributeYearData()
Dim IE As Object
Dim rng As Range
Dim X As Long
Dim Z As Long
Dim Yr As Long
Dim FileNum As Long
Dim myStr As String
Dim sURLdate As String
Dim MonthData() As String
Dim YearParts() As String
Const sURL1 As String = "http://tonto.eia.doe.gov/dnav/ng/hist/n9132cn2m.htm"
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate sURL1
While IE.ReadyState < 4
DoEvents
Wend
myStr = IE.Document.body.innerhtml
Set rng = Range("A2")
YearParts = Split(myStr, "<td class=B4&nbsp;&nbsp;", , vbTextCompare)
For X = 0 To UBound(YearParts) - 1
Yr = Val(YearParts(X + 1))
MonthData = Split(YearParts(X + 1), "<td class=B3", , vbTextCompare)
For Z = 1 To 12
rng.Offset(Z + 12 * X - 1).Value = DateSerial(Yr, Z, 1)
rng.Offset(Z + 12 * X - 1).NumberFormat = "mmm yyyy"
rng.Offset(Z + 12 * X - 1, 1).Value = Val(Replace(MonthData(Z), ",", ""))
Next
Next
End Sub

--
Rick (MVP - Excel)


"Jason" wrote in message ...
Thanks...that gets the info we need, but we want to keep it with the dates in
column A and the totals for each month in column B (it's a legacy
spreadsheet that we have charts based off)

The Sub that Colbert posted does this and I added in some stuff to allow
headers, but the syntax is off a little in the pattern search due to the
numbers having commas

Thanks again though! I am new to this and so I always go through code
people post to try and learn what it is doing.