# How To: Calculate distance to many points in a shapefile from a single point in another shapefile

## Summary

Instructions provided describe how to calculate the distances from all points in a shapefile to a single point in another shapefile.

`Note:Both shapefiles must be in the same Projected Coordinate System, using linear units, e.g., feet or meters.`

## Procedure

The Visual Basic for Applications (VBA) script below calculates the distances from all points in a shapefile to a single point in another shapefile.

1. Copy the script below into Notepad, save the script with the name 'calculate_many_distances.txt', and minimize the Notepad window on the taskbar.

`Code:Dim pMxDoc As IMxDocumentSet pMxDoc = ThisDocumentDim pMap As IMapSet pMap = pMxDoc.FocusMapDim pLayer as IlayerSet pLayer = pMap.Layer(0)Dim pFLayer as IFeatureLayerSet pFLayer = pLayerDim pFClass as IFeatureClassSet pFClass = pFLayer.FeatureClassDim pFeature as IFeatureSet pFeature = pFClass.GetFeature(0)Dim pPointOne As IPointSet pPointOne = pFeature.shapeDim dblX1 as DoubleDim dblY1 as DoubledblX1 = pPointOne.XdblY1 = pPointOne.YDim pGeometry As IGeometrySet pGeometry = [Shape]Dim dblDistance as DoubleDim dblX2 as DoubleDim dblY2 as DoubleIf (Not IsNull([Shape])) Then  Set pGeometry = [Shape]  If (Not pGeometry.IsEmpty) Then    Dim pPointMany As IPoint    Set pPointMany = [Shape]    dblX2 = pPointMany.X    dblY2 = pPointMany.Y    dblDistance = SQR( (dblX1-dblX2)^2 + (dblY1-dblY2)^2 )  End IfEnd If`

2. Add both sets of point data to a new, empty ArcMap session.
3. Right-click the name of the layer that contains many points > Selection > Select All.
4. Open the attribute table for the shapefile that contains many points, and add a field to the table named DISTANCE, defined as a double field.
5. In the attribute table, right-click the field name DISTANCE > Field Calculator.
6. In the Field Calculator, check the 'Advanced' box. Copy the lines from Notepad into the Pre-Logic VBA Script Code window.
7. In the 'Distance =' box , type in dblDistance. Click OK.
8. The distances from the single point are calculated into the DISTANCE field in the attribute table of the shapefile containing many points.
9. The DISTANCE is calculated in the units of the projection; feet or meters. If other distance units such as miles are needed, add a new field to the attribute table, such as MILES, and calculate the units from the DISTANCE field into the new field.

`Note:To convert U. S. Survey feet to miles, multiply DISTANCE*0.0001893939 OR divide DISTANCE/5280To convert meters to miles, multiply DISTANCE*0.0006213711922`