Skip to content

Instantly share code, notes, and snippets.

@mc3k
Created June 9, 2016 23:18
Show Gist options
  • Select an option

  • Save mc3k/44b00bb8194694f12e2cb894f6a62ba0 to your computer and use it in GitHub Desktop.

Select an option

Save mc3k/44b00bb8194694f12e2cb894f6a62ba0 to your computer and use it in GitHub Desktop.
VBA Code to convert Postcode in to OS grid co-ordinates
Sub OSgridPcode()
'Converts postcode into OS x,y
'www.childs.be
'
Dim sURL As String, sHTML As String
Dim oHttp As Object
miles = 6.21371192237334E-04
cellr = ActiveCell.Row
cellc = ActiveCell.Column
Do While Cells(cellr, cellc - 1) <> ""
sURL = "http://streetmap.co.uk/postcode/" + ActiveSheet.Cells(cellr, cellc - 1).Value
On Error Resume Next
Set oHttp = CreateObject("MSXML2.XMLHTTP")
If Err.Number <> 0 Then
Set oHttp = CreateObject("MSXML.XMLHTTPRequest")
MsgBox "Error 0 has occured while creating a MSXML.XMLHTTPRequest object"
End If
If oHttp Is Nothing Then
MsgBox "For some reason I wasn't able to make a MSXML2.XMLHTTP object"
Exit Sub
End If
oHttp.Open "GET", sURL, False
oHttp.Send
sHTML = oHttp.responseText
coordx1 = InStr(1, sHTML, "SM_locationX", vbTextCompare)
coordx2 = InStr(coordx1, sHTML, ";", vbTextCompare)
coordy1 = InStr(1, sHTML, "SM_locationY", vbTextCompare)
coordy2 = InStr(coordy1, sHTML, ";", vbTextCompare)
coordx = Mid(sHTML, coordx1 + 15, coordx2 - coordx1 - 15)
coordy = Mid(sHTML, coordy1 + 15, coordy2 - coordy1 - 15)
Cells(cellr, cellc).Value = coordx * miles
Cells(cellr, cellc + 1).Value = coordy * miles
cellr = cellr + 1
Loop
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment