Hello everyone!
I am trying to import data from a webpage. I have written some code that works, but because of the amount of data I am trying to upload i really need to streamline the code. I am looking for any suggestions.
The website i am trying to upload information from:
http://www4.ncdc.noaa.gov/cgi-win/wwcgi.dll?wwevent~ShowEvent~842907
You can see that the end of the URL is a number. This number corresponds to an event number....there are about 850,000 of them! And I need to import all of them...
I know that one excel workbook can not hold all of the lines, so the code will have to upload as many as possible and then open a new workbook and continue.
If you go through a few of the events (just increase or decrease the event number, i.e. 842906 instead of 842907 in the URL above.) you will see that there are different event types. I am only interested in Hail and Wind events. There are several different types of wind events...thunderstorm wind, high wind, etc. (You can go to this website and look at some of the events: http://www4.ncdc.noaa.gov/cgi-win/wwcgi.dll?wwEvent~storms You will have to pick a state. You will then see a new page where you enter some additional data and see the different event types). I thought that the code could import maybe 1000 events, remove all of the non-hail/wind events, and then continue. This would reduce the number of lines that would have to be in each workbook.
I have inserted the code I have so far below. You will see that the only fields that I need from the webpage are: event type, event date, lat/long, magnitude, state, and county. I wouldn't mind getting everything and then removing unnecessary fields later.
The code uses the "Data>From Web" type of import (i am sure there is a name for this...i just dont know what it is.). Once the data is in the workbook, I copy the fields of interest into new cells. I then move on to the next event number, and repeat, adding a new row of data each time.
Please let me know if you can help! Thanks in advance.
Code:
Sub Macro1()
Dim eventtype
Dim eventdate
Dim latlong
Dim mag
Dim state
Dim county
eventnum = 1
rownum = 14
Do
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www4.ncdc.noaa.gov/cgi-win/wwcgi.dll?wwevent~ShowEvent~" & eventnum, _
Destination:=Range("$A$1"))
.Name = "wwcgi.dll?wwevent~ShowEvent~866281_1"
.FieldNames = True
.RowNumbers = Flase
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
eventtype = Range("B1").Value
eventdate = Range("B2").Value
latlong = Range("B4").Value
mag = Range("B7").Value
state = Range("D1").Value
county = Range("D3").Value
Cells(rownum, 1).Value = eventnum
Cells(rownum, 2).Value = eventtype
Cells(rownum, 3).Value = eventdate
Cells(rownum, 4).Value = latlong
Cells(rownum, 5).Value = mag
Cells(rownum, 6).Value = state
Cells(rownum, 7).Value = county
rownum = rownum + 1
eventnum = eventnum + 1
Loop While eventnum < 5 'this will have to be something like 850,000 when code is complete
End Sub
I am trying to import data from a webpage. I have written some code that works, but because of the amount of data I am trying to upload i really need to streamline the code. I am looking for any suggestions.
The website i am trying to upload information from:
http://www4.ncdc.noaa.gov/cgi-win/wwcgi.dll?wwevent~ShowEvent~842907
You can see that the end of the URL is a number. This number corresponds to an event number....there are about 850,000 of them! And I need to import all of them...
I know that one excel workbook can not hold all of the lines, so the code will have to upload as many as possible and then open a new workbook and continue.
If you go through a few of the events (just increase or decrease the event number, i.e. 842906 instead of 842907 in the URL above.) you will see that there are different event types. I am only interested in Hail and Wind events. There are several different types of wind events...thunderstorm wind, high wind, etc. (You can go to this website and look at some of the events: http://www4.ncdc.noaa.gov/cgi-win/wwcgi.dll?wwEvent~storms You will have to pick a state. You will then see a new page where you enter some additional data and see the different event types). I thought that the code could import maybe 1000 events, remove all of the non-hail/wind events, and then continue. This would reduce the number of lines that would have to be in each workbook.
I have inserted the code I have so far below. You will see that the only fields that I need from the webpage are: event type, event date, lat/long, magnitude, state, and county. I wouldn't mind getting everything and then removing unnecessary fields later.
The code uses the "Data>From Web" type of import (i am sure there is a name for this...i just dont know what it is.). Once the data is in the workbook, I copy the fields of interest into new cells. I then move on to the next event number, and repeat, adding a new row of data each time.
Please let me know if you can help! Thanks in advance.
Code:
Sub Macro1()
Dim eventtype
Dim eventdate
Dim latlong
Dim mag
Dim state
Dim county
eventnum = 1
rownum = 14
Do
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www4.ncdc.noaa.gov/cgi-win/wwcgi.dll?wwevent~ShowEvent~" & eventnum, _
Destination:=Range("$A$1"))
.Name = "wwcgi.dll?wwevent~ShowEvent~866281_1"
.FieldNames = True
.RowNumbers = Flase
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
eventtype = Range("B1").Value
eventdate = Range("B2").Value
latlong = Range("B4").Value
mag = Range("B7").Value
state = Range("D1").Value
county = Range("D3").Value
Cells(rownum, 1).Value = eventnum
Cells(rownum, 2).Value = eventtype
Cells(rownum, 3).Value = eventdate
Cells(rownum, 4).Value = latlong
Cells(rownum, 5).Value = mag
Cells(rownum, 6).Value = state
Cells(rownum, 7).Value = county
rownum = rownum + 1
eventnum = eventnum + 1
Loop While eventnum < 5 'this will have to be something like 850,000 when code is complete
End Sub