sjoerdbosch
New Member
- Joined
- Mar 27, 2023
- Messages
- 18
- Office Version
- 365
- Platform
- Windows
I am trying to have users entering a specific format into a cell using a decimal and not a comma as separator.
A very helpful member got this far, but unfortunately it is not working for me.
I need the following entered into a cell [F15] for example: 16-15.2 N and in cell F16 for example 008-12.6 E - NOT 16-15,2 N [with a comma instead of a decimal point.
These are coordinates and used for noting down a position at certain times. F15 is Latitude and F16 is longitude - Lat is either N or S and Longitude is either E or W
In the below code I am getting an error if entered what I want and it works if I use a comma instead of a decimal.
Also the letters N.S.E.W should be in upper case
I could also use a formula in data what forces the specific format.
I am using excel 365 and 2016 - on both versions it does the same
If anyone could please assist - much appreciated
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lati As Range, longi As Range, cell As Range, s, s1
Set lati = Range("F15") ' change to actual cells
Set longi = Range("F16") ' change to actual cells
If Intersect(Target, Union(lati, longi)) Is Nothing Then Exit Sub
For Each cell In Target
On Error GoTo z
s = Split(cell, "-"): s1 = Split(s(1))
If Not Intersect(cell, lati) Is Nothing Then
If s(0) > 90 Or s1(0) > 99.9 Or Len(s1(0)) <> 4 Or (s1(1) <> "N" And s1(1) <> "S") Then GoTo z
Else
If s(0) > 180 Or s1(0) > 99.9 Or Len(s1(0)) <> 4 Or (s1(1) <> "E" And s1(1) <> "W") Then GoTo z
End If
GoTo y
z:
Application.EnableEvents = False
cell.ClearContents
Application.EnableEvents = True
MsgBox "Must be 90-99.9 N/S (latitude) or 180-99.9 E/W (longitude)"
y:
Next
End Sub
A very helpful member got this far, but unfortunately it is not working for me.
I need the following entered into a cell [F15] for example: 16-15.2 N and in cell F16 for example 008-12.6 E - NOT 16-15,2 N [with a comma instead of a decimal point.
These are coordinates and used for noting down a position at certain times. F15 is Latitude and F16 is longitude - Lat is either N or S and Longitude is either E or W
In the below code I am getting an error if entered what I want and it works if I use a comma instead of a decimal.
Also the letters N.S.E.W should be in upper case
I could also use a formula in data what forces the specific format.
I am using excel 365 and 2016 - on both versions it does the same
If anyone could please assist - much appreciated
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lati As Range, longi As Range, cell As Range, s, s1
Set lati = Range("F15") ' change to actual cells
Set longi = Range("F16") ' change to actual cells
If Intersect(Target, Union(lati, longi)) Is Nothing Then Exit Sub
For Each cell In Target
On Error GoTo z
s = Split(cell, "-"): s1 = Split(s(1))
If Not Intersect(cell, lati) Is Nothing Then
If s(0) > 90 Or s1(0) > 99.9 Or Len(s1(0)) <> 4 Or (s1(1) <> "N" And s1(1) <> "S") Then GoTo z
Else
If s(0) > 180 Or s1(0) > 99.9 Or Len(s1(0)) <> 4 Or (s1(1) <> "E" And s1(1) <> "W") Then GoTo z
End If
GoTo y
z:
Application.EnableEvents = False
cell.ClearContents
Application.EnableEvents = True
MsgBox "Must be 90-99.9 N/S (latitude) or 180-99.9 E/W (longitude)"
y:
Next
End Sub