View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
ryguy7272 ryguy7272 is offline
external usenet poster
 
Posts: 2,836
Default Loop through date range, but skip down to next year


I can't tell if this is what you want or not, but the code takes all the data
from that site and sweeps it into two columns. Try it and you will see:
Sub settupp()
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
s1.Activate
n = Cells(Rows.Count, 1).End(xlUp).Row
k = 1
For i = 1 To n
namee = Cells(i, 1).Value
For j = 1 To 13
numberr = Cells(i, j).Value
s2.Cells(k, 1) = namee
s2.Cells(k, 2) = numberr
k = k + 1
Next
Next
End Sub

Also, just for fun, here is a recorded macro that imports the data from that
site:
Sub Macro1()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://tonto.eia.doe.gov/dnav/ng/hist/n9132cn2m.htm",
Destination:=Range _
("A1"))
.Name = "n9132cn2m"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub

You probably know that already.............

HTH,
Ryan---

--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Jason" wrote:

Okay so the title is stupid, but I don't know how to explain in just a few
words my issue.

We have a range of dates in Column A
Jan 1973
Feb 1973
...
Apr 2009

I am trying to pull numbers from a website that correspond to totals for
each month, but the website (
http://tonto.eia.doe.gov/dnav/ng/hist/n9132cn2m.htm) only lists the years,
and then each line below it corresponds to a total for each month in that
year. Here is an example of the source for year 1973:

<tr
<td class='B4' 1973</td
<td class='B3'60</td
<td class='B3'384</td
<td class='B3'1,167</td
<td class='B3'931</td
<td class='B3'1,670</td
<td class='B3'1,598</td
<td class='B3'1,758</td
<td class='B3'1,829</td
<td class='B3'1,022</td
<td class='B3'1,465</td
<td class='B3'1,483</td
<td class='B3'1,456</td
</tr

I used the following code to test getting the January total for each year,
but how can I set my loop to skip down 12 cells to the next year? Currently
it is putting the January value in each cell for each year

Set rng = Range("A2").CurrentRegion
Set rng = rng.Resize(rng.Rows.Count, 1)

For Each c In rng
sURLdate = Format(c.Value2, "yyyy")
c.Offset(0, 1).Value = RegexMid(myStr, sURLdate, "class=b3")