Userform code to open UK post code within Google Maps

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,602
Office Version
  1. 2007
Platform
  1. Windows
On my userform i have TextBox3 which the user enters the customers Post Code.

On the same userform i have a commandbutton called OpenGoogleMaps.

What i am looking for is when the command button is pressed the code looks at the entry in TextBox3 & opens that Post Code in Google Maps.

The is the current code that is on the button for opening Google Maps but not sure how to proceed.

VBA Code:
Private Sub OpenGoogleMaps_Click()
ActiveWorkbook.FollowHyperlink Address:="https://www.google.co.uk/maps", NewWindow:=True
End Sub

So basically look in TextBox2 value = BS9 2HH
Now open BS9 2HH in Google Maps.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
A form i use has the feature which its code is this,


Does this help with how we write the code ?

At present GL2 4AR is the value in TextBox3
 
Upvote 0
This works on the command button.
Just need to write the code for it to get the post code from TextBox3 & apply it to the Google maps code on command button

VBA Code:
Private Sub OpenGoogleMaps_Click()
ActiveWorkbook.FollowHyperlink Address:="https://www.google.co.uk/maps/place/GL2+4AR", NewWindow:=True
End Sub
 
Upvote 0
VBA Code:
Private Sub OpenGoogleMaps_Click()
    Dim postcode As String
    postcode = Split(Me.TextBox3.Value, " ")(0) & "+" & Split(Me.TextBox3.Value, " ")(1)
    ActiveWorkbook.FollowHyperlink Address:="https://www.google.co.uk/maps/place/" & postcode, NewWindow:=True
End Sub
 
Upvote 0
Solution
Hi,
Nearly works.

I enter in TextBox3 BS9 2HH & run the command button.
Google opens to BS9 which is a large area where BS9 2HH pinpoints it more to the street in question

You helped me out before with a post code & i assumed you were in the UK.
So UK post codes are like this,

BS9 2HH or say BS23 4HT

Does the help to edit the code supplied above to open like what was typed in the Textbox3

Thanks

When i entered BS9 2HH the url is as shown.
BS9 · Bristol, UK so its missing the +2HH part
 
Upvote 0
Just to advise as i added a bit of code on the end.

VBA Code:
Private Sub OpenGoogleMaps_Click()

    Dim postcode As String
    postcode = Split(Me.TextBox3.Value, " ")(0) & "+" & Split(Me.TextBox3.Value, " ")(1) & "+"
    ActiveWorkbook.FollowHyperlink Address:="https://www.google.co.uk/maps/place/" & postcode, NewWindow:=True

End Sub

If i type this format post code BS29 6HD 7 characters
Then it works.

But it doesnt for when i try this format BS9 2HH 6 characters
 
Upvote 0
From post#3
This works on the command button.
Just need to write the code for it to get the post code from TextBox3 & apply it to the Google maps code on command button

VBA Code:
Private Sub OpenGoogleMaps_Click()
ActiveWorkbook.FollowHyperlink Address:="https://www.google.co.uk/maps/place/GL2+4AR", NewWindow:=True
End Sub
The code of post#4 does that no matter if you enter a 3-space-3 or 4-space-3 postal code.

I enter in TextBox3 BS9 2HH & run the command button.
Google opens to BS9 which is a large area where BS9 2HH pinpoints it more to the street in question
I can only reproduce that when there is more than 1 space in the middle of the postal code

Just to advise as i added a bit of code on the end.
Why you would do that

If i type this format post code BS29 6HD 7 characters
Then it works.

But it doesnt for when i try this format BS9 2HH 6 characters
They both work for me with the code of post#4
 
Upvote 0
So i have used the code from post #4
For me it would only work for these style post codes BS29 6HD, BS24 7RT, TQ12 4JX

I tried the other post codes BUT all only went to the first part of the post code.
Example, i type BS9 2HH but it only pulled up BS9 when Google opened.

So this code is for TextBox3
VBA Code:
Private Sub TextBox3_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.TextBox3) > 0 Then
        Me.TextBox3.Value = Format(Replace(Me.TextBox3.Value, " ", ""), "@@@@ @@@")
    End If
End Sub

If i remove that code it works fine for both of them every time.

Put this code on your Textbox & see if it then goes wrong

Type BS9 2HH & see if Google return this,

EaseUS_2024_09_ 2_18_45_40.jpg
 
Upvote 0
try adding the trim function to _BeforeUpdate

VBA Code:
Private Sub TextBox3_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.TextBox3) > 0 Then
        Me.TextBox3.Value = Trim(Format(Replace(Me.TextBox3.Value, " ", ""), "@@@@ @@@"))
    End If
End Sub
 
Upvote 0
That did the trick & works fine.

Many thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top