This code gets the data from the website
http://api.yr.no/weatherapi/location...5;lon=8.555967
The information obtained from this website is then organized in a spreadsheet. I am trying to avoid crashing the program. I've noticed that if an incorrect latitude/longitude is input then the website returns an error. What would need to be added in these codes to have the code check if an error is produced and then ask the user to check their input BEFORE adding the input into the "Site List" worksheet.
Private Sub CommandButtonEditUpdate_Click()
Dim Msg As String
If ComboBoxEditSiteName.ListIndex < 0 Then
MsgBox "You must select a site to edit"
Else
If TextBoxEditSiteName = "" Then
Msg = Msg & "You must include a site name" & vbCrLf
End If
If TextBoxEditLat = "" Then
Msg = Msg & "You must include a latitude" & vbCrLf
ElseIf Not IsNumeric(TextBoxEditLat) Then
Msg = Msg & "Latitude must be numeric" & vbCrLf
End If
If TextBoxEditLong = "" Then
Msg = Msg & "You must include a longitude" & vbCrLf
ElseIf Not IsNumeric(TextBoxEditLong) Then
Msg = Msg & "Longitude must be numeric" & vbCrLf
End If
If Msg <> "" Then
MsgBox Msg
Else
' locate site record
' Write site name to cell
' Build URL from lat and long and write to cell
' clear form and leave form showing
Dim selRow As Long
Dim lat1 As String
Dim lon1 As String
Dim sitelink1 As String
Dim sitename1 As String
'Assign variables to construct URL
selRow = ComboBoxEditSiteName.ListIndex + 1
lat1 = Format(cpUserForm.TextBoxEditLat.Value, "0.0000000000000000")
lon1 = Format(cpUserForm.TextBoxEditLong.Value, "0.0000000000000000")
sitelink1 = "http://api.yr.no/weatherapi/locationforecastlts/1.2/?lat=" & lat1 & ";lon=" & lon1
sitename1 = cpUserForm.TextBoxEditSiteName.Value
'Write values to spreadsheet cells.
Worksheets("Site List").Cells(selRow, 1) = sitelink1
Worksheets("Site List").Cells(selRow, 2) = sitename1
Worksheets("Site List").Cells(selRow, 3) = lat1
Worksheets("Site List").Cells(selRow, 4) = lon1
AddClear
LoadComboBoxEditSiteName
LoadListBoxDeletePickSiteList
EditClear
End If
End If
End Sub
Private Sub CommandButtonAddUpdate_Click()
Dim Msg As String
If TextBoxAddSiteName = "" Then
Msg = Msg & "You must include a site name" & vbCrLf
End If
If TextBoxAddLat = "" Then
Msg = Msg & "You must include a latitude" & vbCrLf
ElseIf Not IsNumeric(TextBoxAddLat) Then
Msg = Msg & "Latitude must be numeric" & vbCrLf
End If
If TextBoxAddLong = "" Then
Msg = Msg & "You must include a longitude" & vbCrLf
ElseIf Not IsNumeric(TextBoxAddLong) Then
Msg = Msg & "Longitude must be numeric" & vbCrLf
End If
If Msg <> "" Then
MsgBox Msg
Else
' Create a new site record
' Write site name to cell
' Build URL from lat and long and write to cell
' clear form and leave form showing
Dim emptyRow As Long
Dim lat As String
Dim lon As String
Dim siteLink As String
Dim siteName As String
'Assign variables to construct URL
emptyRow = Sheets("Site List").Cells(Rows.Count, 2).End(xlUp).Offset(1).Row
lat = Format(cpUserForm.TextBoxAddLat.Value, "0.0000000000000000")
lon = Format(cpUserForm.TextBoxAddLong.Value, "0.0000000000000000")
siteLink = "http://api.yr.no/weatherapi/locationforecastlts/1.2/?lat=" & lat & ";lon=" & lon
siteName = cpUserForm.TextBoxAddSiteName.Value
'Add values to site list worksheet
Worksheets("Site List").Cells(emptyRow, 1) = siteLink
Worksheets("Site List").Cells(emptyRow, 2) = siteName
Worksheets("Site List").Cells(emptyRow, 3) = lat
Worksheets("Site List").Cells(emptyRow, 4) = lon
'Refresh lists
AddClear
LoadComboBoxEditSiteName
LoadListBoxDeletePickSiteList
End If
End Sub
Bookmarks