VBA: Calculate the distance between two cities in a board

Ruben1629

New Member
Joined
Dec 7, 2021
Messages
2
Office Version
  1. 2019
Hi guys,

I have a board with a list of cities and I need to create a distance function on VBA that would calculate the distance between these two cities.

I am having a hard time finding where to start, any help would be welcomed :)

Here is the board:


AngersBordeauxCaenCalaisLilleLyonMarseilleNantesNiceParisRennesStrasbourgToulouse
Villes1Angers
0​
398​
255​
512​
521​
597​
905​
91​
1062​
297​
127​
777​
637​
Bordeaux
398​
0​
617​
874​
806​
556​
645​
353​
802​
590​
472​
945​
246​
Caen
255​
617​
0​
347​
388​
693​
1002​
290​
1159​
234​
183​
739​
845​
Calais
512​
874​
347​
0​
110​
759​
1069​
598​
1226​
293​
526​
621​
963​
Lille
521​
806​
388​
110​
0​
691​
1001​
600​
1158​
225​
569​
554​
895​
Lyon
597​
556​
693​
759​
691​
0​
314​
684​
471​
466​
740​
487​
537​
Marseille
905​
645​
1002​
1069​
1001​
314​
0​
985​
199​
775​
1050​
798​
403​
Nantes
91​
353​
290​
598​
600​
684​
985​
0​
1143​
385​
113​
865​
586​
Nice
1062​
802​
1159​
1226​
1158​
471​
199​
1143​
0​
932​
1207​
785​
560​
Paris
297​
590​
234​
293​
225​
466​
775​
385​
932​
0​
353​
491​
679​
Rennes
127​
472​
183​
526​
569​
740​
1050​
113​
1207​
353​
0​
830​
700​
Strasbourg
777​
945​
739​
621​
554​
487​
798​
865​
785​
491​
830​
0​
971​
Toulouse
637​
246​
845​
963​
895​
537​
403​
586​
560​
679​
700​
971​
0​
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
The data in your post was misaligned. Try the macro on the data below:
Villes1AngersBordeauxCaenCalaisLilleLyonMarseilleNantesNiceParisRennesStrasbourgToulouse
Angers0398255512521597905911062297127777637
Bordeaux3980617874806556645353802590472945246
Caen255617034738869310022901159234183739845
Calais512874347011075910695981226293526621963
Lille521806388110069110016001158225569554895
Lyon5975566937596910314684471466740487537
Marseille90564510021069100131409851997751050798403
Nantes9135329059860068498501143385113865586
Nice1062802115912261158471199114309321207785560
Paris2975902342932254667753859320353491679
Rennes127472183526569740105011312073530830700
Strasbourg7779457396215544877988657854918300971
Toulouse6372468459638955374035865606797009710


VBA Code:
Option Compare Text
Sub getDistance()
    Application.ScreenUpdating = False
    Dim LastRow As Long, city1 As String, city2 As String, fnd1 As Range, fnd2 As Range
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    city1 = InputBox("Enter the name of the first city.")
    If city1 = "" Then Exit Sub
    Set fnd1 = Rows(1).Find(city1, LookIn:=xlValues, lookat:=xlWhole)
    If fnd1 Is Nothing Then
         MsgBox (city1 & " was not found.  Please try again.")
         Exit Sub
    End If
    city2 = InputBox("Enter the name of the second city.")
    If city2 = "" Then Exit Sub
    If city1 = city2 Then
        MsgBox ("You have entered the same 'start' and 'end' cities." & Chr(10) & "Please try again.")
        Exit Sub
    End If
    Set fnd2 = Range("A:A").Find(city2, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd2 Is Nothing Then
        MsgBox ("The distance between " & city1 & " " & city2 & " is " & Cells(fnd2.Row, fnd1.Column) & " kilometers.")
    Else
        MsgBox (city2 & " was not found.  Please try again.")
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
The data in your post was misaligned. Try the macro on the data below:
Villes1AngersBordeauxCaenCalaisLilleLyonMarseilleNantesNiceParisRennesStrasbourgToulouse
Angers0398255512521597905911062297127777637
Bordeaux3980617874806556645353802590472945246
Caen255617034738869310022901159234183739845
Calais512874347011075910695981226293526621963
Lille521806388110069110016001158225569554895
Lyon5975566937596910314684471466740487537
Marseille90564510021069100131409851997751050798403
Nantes9135329059860068498501143385113865586
Nice1062802115912261158471199114309321207785560
Paris2975902342932254667753859320353491679
Rennes127472183526569740105011312073530830700
Strasbourg7779457396215544877988657854918300971
Toulouse6372468459638955374035865606797009710


VBA Code:
Option Compare Text
Sub getDistance()
    Application.ScreenUpdating = False
    Dim LastRow As Long, city1 As String, city2 As String, fnd1 As Range, fnd2 As Range
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    city1 = InputBox("Enter the name of the first city.")
    If city1 = "" Then Exit Sub
    Set fnd1 = Rows(1).Find(city1, LookIn:=xlValues, lookat:=xlWhole)
    If fnd1 Is Nothing Then
         MsgBox (city1 & " was not found.  Please try again.")
         Exit Sub
    End If
    city2 = InputBox("Enter the name of the second city.")
    If city2 = "" Then Exit Sub
    If city1 = city2 Then
        MsgBox ("You have entered the same 'start' and 'end' cities." & Chr(10) & "Please try again.")
        Exit Sub
    End If
    Set fnd2 = Range("A:A").Find(city2, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd2 Is Nothing Then
        MsgBox ("The distance between " & city1 & " " & city2 & " is " & Cells(fnd2.Row, fnd1.Column) & " kilometers.")
    Else
        MsgBox (city2 & " was not found.  Please try again.")
    End If
    Application.ScreenUpdating = True
End Sub
it worked, thank you so much!
 
Upvote 0
You are very welcome. :)
 
Upvote 0
If your interested, you could also do this with a formula
+Fluff 1.xlsm
ABCDEFGHIJKLMNOPQ
1Villes1AngersBordeauxCaenCalaisLilleLyonMarseilleNantesNiceParisRennesStrasbourgToulouse
2Angers0398255512521597905911062297127777637Caen1159
3Bordeaux3980617874806556645353802590472945246Nice
4Caen255617034738869310022901159234183739845
5Calais512874347011075910695981226293526621963
6Lille521806388110069110016001158225569554895
7Lyon5975566937596910314684471466740487537
8Marseille90564510021069100131409851997751050798403
9Nantes9135329059860068498501143385113865586
10Nice1062802115912261158471199114309321207785560
11Paris2975902342932254667753859320353491679
12Rennes127472183526569740105011312073530830700
13Strasbourg7779457396215544877988657854918300971
14Toulouse6372468459638955374035865606797009710
Data
Cell Formulas
RangeFormula
Q2Q2=INDEX(B2:N14,MATCH(P2,A2:A14,0),MATCH(P3,B1:N1,0))
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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