I'm trying to download data from a weather website (weather.gov) and trying to use variables for the latitude and longitude in the URL. If I go to the website and switch out the latitude and longitude manually, I will be directed to the correct city's weather forecast. However, when I try to set the latitude and longitude as a variable and combine the part of the URL that doesn't change with the variables for each city, it give me a compile error, Expected: list separator or ). It keeps getting stuck on this section ("&lon=-") in the URL. I'm not sure if there's a better way to declare the variable or add them in, but it doesn't make much sense to me why it doesn't like that middle section. Code below. Thanks!
P.S. The cityLat and cityLong variables would be values based on the city, but for right now I just have the actual latitude and longitude just to test it with.
Sub forecast_weather()
Dim cityLon As String
Dim cityLat As String
cityLat = "41.8781"
cityLong = "87.6298"
ActiveWorkbook.Worksheets("Chicago Weather").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://forecast.weather.gov/MapClick.php?lat="&cityLat&"&lon=-
"&cityLong&"&unit=0&lg=english&FcstType=text&TextType=2", _
Destination:=Range("$A$1"))
.Name = "q?s=usdCAd=x_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
You need spaces around the &
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://forecast.weather.gov/MapClick.php?lat=" & cityLat & _
"&lon=" & cityLong & "&unit=0&lg=english&FcstType=text&TextType=2", _
Destination:=Range("$A$1"))
this gets Chicago weather
Sub forecast_weather()
Dim cityLon As String
Dim cityLat As String
cityLat = "41.8781"
cityLong = "-87.6298"
With Worksheets("sheet1").QueryTables.Add( _
Connection:="URL;http://forecast.weather.gov/MapClick.php?" _
& "lat=" & cityLat _
& "&lon=" & cityLong _
& "&unit=0&lg=english&FcstType=text&TextType=2", Destination:=Range("$A$1"))
.Name = "q?s=usdCAd=x_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
Related
I am new to VBA.
I am trying to extract all columns from other file but the result is displayed with error 13. How can I solve it?
Sub generate_data()
Application.CutCopyMode = False
Sheets("Output").Activate
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=HKGHKGT_UBET;Description=HKGHKGT_UBET;UID=SiuH;Trusted_Connection=Yes;APP=Microsoft Office 2016;WSID=HKHKDNO304NB;DATABASE=" _
), Array("UBet")), Destination:=Range("$A$1")).QueryTable
'CommandType = 0
.CommandText = Array("select * from UBet..cathy_fin where duns_no in(" & Sheets("Main").Range("A2").Value & ") order by stmt_date desc")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "HKGHKGT_UBET"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.ListObjects("HKGHKGT_UBET").TableStyle = ""
Columns("B:B").Select
Selection.NumberFormat = "m/d/yyyy"
End Sub
Extracting tabular form data from multiple webpages into an excel using VBA macros..!! Currently iam using below link but i could able to only one webpage in the code..i have list of ulr's to get data from...and it has to come in vertical..!! please suggest me.. :)
Sub INDEXdata()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://recorder.maricopa.gov/recdocdata/GetRecDataDetail.aspx?rec=19770000007" _
, Destination:=Range("$A$1"))
.Name = "rec=19770000006"
.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 = "2,3"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
OK, I don't know how much programming background you have, and I also don't know what parts of the code you posted are specific to that one source url and target location.
But, something like this might work. I made the assumption that the url, the destination, and the name would change for each page you wanted to pull from.
What I did was take the part of the code that looked like it would be true for all of the source pages and destinations, and put that in its own parameterized subroutine. The original routine IndexData just specifies the URL and the destination, and the name, for each copy operation.
Sub IndexData()
GetData("http://recorder.maricopa.gov/recdocdata/GetRecDataDetail.aspx?rec=19770000007" , _
"$A$1", _
"rec=19770000006")
GetData("http://somewhereelse.com/somedata.aspx?rec=12345", _
"$A$2", _
"rec=12345")
GetData("http://anotherurl.com/etc", _
"$A$3", _
"something")
End
Sub GetData(url as string, destination as string, name as string)
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & url , Destination:=Range(destination))
.Name = name
.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 = "2,3"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
I have this VBA script that works:
Sub Basic_Web_Query()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & ActiveSheet.Range("A2").Value, Destination:=Range("E2"))
.Name = "q?s=goog_2"
.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 = "2"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
I want the macro to do this in a loop, i.e. go to A3 with destination E3, A4 with destination A4... etc.
any help will be appreciated
thansk!
I like to use 'offset' for these loops: OFFSET(rows down, columns right).
Negative values move up and left.
for example:
for p= 1 to 5
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & ActiveSheet.Range("A2").offset(p-1,0).Value, Destination:=Range("E2").offset(p-1,0))
'other code
End With
next p
google 'loop vba' and you will hit plenty of examples. Here's one:
Dim rw As Long
For rw = 1 To 5
Range("E" & rw).Value = Range("A" & rw).Value
'other stuff
Next rw
I would like to download the daily prices of our stock exchange using the following code in vba. Although the code works i cant seem to be able to get the sheets renamed to the corresponding day when the price list was obtained.
Dim DownloadDay As Date
DownloadDay = #3/4/2014#
Do While DownloadDay < #4/4/2014#
ActiveWorkbook.Worksheets.Add
Call website(Format(DownloadDay, "YYYYMMDD"))
'INCREMENT THE DAY
Sheets.Add.Name = "DownloadDay"
DownloadDay = DownloadDay + 1
Loop
End Sub
Sub website(sDate As String)
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://live.mystocks.co.ke/price_list/" & DownloadDay & "/", Destination:=Range("$A$1"))
.Name = DownloadDay
'To rename each work sheet with the corresponding day'
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
This line of VBA will set the name of your worksheet:
Sheets("Sheet2").Name = "NewName"
You can not use "/" the character in the name though, and the variable is set to a date not a string.
Try this.
.name = CSTR(FORMAT(DownloadDay,"YYYYMMMDD"))
I have created a sub in VBA which checks if I have been undercut on an online bookstore. The problem is, many authors and titles have accented characters in them, which, as far as I have experienced, I cant but into a querytable. For example the charactera á,é,ö become strange symbols like # or $ when I try to load a webpage.
I am attaching the code I have so far:
For i = 1 To Sorok
Nev(i) = Ekezet(Sheets(AdatokLap).Cells(i + 1, 4)
KonyvCim(i) = Ekezet(Sheets(AdatokLap).Cells(i + 1, 5)
ConnectString = "URL;http://bookline.hu/search/search.action? inner=true&searchfield=" & Nev(i) & " " & KonyvCim(i) & "&tab=bookline.hu%2Foldbook"
Set qt = Sheets(QueryLap).QueryTables.Add(Connection:=ConnectString, Destination:=Cells(1, 1))
With qt
.Name = "MyName"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
I have written a function, called Ekezet(), which removes all accented characters from a string. Is there any way I don't have to remove accented characters and still run a web query?