VBA & Macro Problems

Fix Excel VBA Latitude and Longitude Conversion to Decimal Degrees

Huma Ashraf ChHuma Ashraf Ch Nov 13, 2026 870 views

Question details

The user needs a correct VBA calculation for converting latitude and longitude from degrees, minutes, and seconds to decimal degrees while preserving negative coordinates.

Fix Excel VBA Latitude and Longitude Conversion to Decimal Degrees
Product
Microsoft Excel VBA and Microsoft Learn documentation
Device & OS
not provided
Scenario
Using a VBA example from a Microsoft article to convert a coordinate such as -77°04'43" to decimal degrees.
Observed behavior
The example produces an incorrect positive result for a negative coordinate. The expected result is approximately -77.078611111.
Before you start

Identify whether your source coordinates use a leading minus sign or N/S/E/W direction letters. If you will add a VBA function to Excel, use a macro-enabled workbook (.xlsm) and enable macros only for code you trust.

Solution 1Recommended

Apply the coordinate sign to the entire VBA conversion

Use this corrected function when degrees, minutes, and seconds are available as separate numeric values.

Calculate the magnitude as Abs(degrees) + minutes / 60 + seconds / 3600, then apply the sign to that entire value. For southern or western coordinates, the whole result must be negative—not just the degrees component.

This function accepts separate numeric components rather than parsing a complete DMS text string. Its optional Negative argument also handles coordinates below one degree, where a numeric degrees value of zero cannot retain a minus sign.

1
Open the VBA editor

In Excel, open Developer > Visual Basic. If Developer is hidden, enable it through Excel's ribbon customization settings. In the VBA editor, select your workbook and choose Insert > Module.

2
Add the corrected function

Paste this function into the standard module: Public Function DMSDecimal(ByVal Degrees As Double, _ ByVal Minutes As Double, _ ByVal Seconds As Double, _ Optional ByVal Negative As Boolean = False) As Variant Dim Magnitude As Double If Minutes < 0 Or Minutes >= 60 Or Seconds < 0 Or Seconds >= 60 Then DMSDecimal = CVErr(xlErrNum) Exit Function End If Magnitude = Abs(Degrees) + Minutes / 60# + Seconds / 3600# If Degrees < 0 Or Negative Then DMSDecimal = -Magnitude Else DMSDecimal = Magnitude End If End Function

3
Test the negative coordinate

Return to an Excel worksheet and enter =DMSDecimal(-77,4,43) in a cell. The result should be approximately -77.078611111. Enter =DMSDecimal(77,4,43) to confirm that the positive equivalent remains positive.

4
Handle direction letters and zero-degree coordinates

For a coordinate marked W or S, pass TRUE as the fourth argument, such as =DMSDecimal(77,4,43,TRUE). For 0°30'00" W, enter =DMSDecimal(0,30,0,TRUE), which returns -0.5. Use nonnegative minutes and seconds.

Apply the coordinate sign to the entire VBA conversion
Check coordinate ranges separately: The function checks minutes and seconds but does not enforce geographic limits. Validate the final latitude within -90 to 90 degrees and longitude within -180 to 180 degrees.
Free Microsoft Office alternative

Convert Coordinate Values with Formulas in WPS Spreadsheet

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Excel format compatibility for easy migration. For this calculation, use a worksheet formula in WPS Spreadsheet instead of VBA; WPS does not repair the Microsoft Learn example or guarantee support for your Excel macros.

Convert separate DMS values with a worksheet formula such as =SIGN(A2)*(ABS(A2)+B2/60+C2/3600) when A2 contains nonzero signed degrees.Open and edit Microsoft Excel .xlsx and .xls files.Use familiar cells, formulas, and number-formatting controls to review coordinate results.Move formula-based coordinate worksheets between compatible spreadsheet applications without relying on VBA.
microsoft office alternative - wps office

Frequently Asked Questions

What should -77°04'43" equal in decimal degrees?

It equals -(77 + 4/60 + 43/3600), or approximately -77.078611111. In Excel, enter =DMSDecimal(-77,4,43) after adding the function above, or enter =-(77+4/60+43/3600) directly.

Why is applying the minus sign only to the degrees incorrect?

A negative coordinate's sign applies to its entire magnitude. Adding positive minutes and seconds to negative degrees moves the result toward zero. Instead, calculate the magnitude using positive components and negate the complete sum.

How do I convert a western coordinate with zero degrees?

A numeric zero cannot preserve a negative sign. Use the explicit direction argument: =DMSDecimal(0,30,0,TRUE) returns -0.5 for 0°30'00" W. Southern coordinates use the same negative-sign convention.

Can I convert DMS values without enabling macros?

Yes. Enter =-(77+4/60+43/3600) in an Excel or WPS Spreadsheet cell for the example coordinate. For reusable columns, store the magnitude of degrees, minutes, and seconds in A2:C2 and a sign of 1 or -1 in D2, then enter =D2*(ABS(A2)+B2/60+C2/3600).

Why does Excel show fewer decimal places than the expected result?

The cell's number format may round the displayed value. Select the result cell and use Home > Increase Decimal to display more decimal places. This changes the display, not the underlying calculation.