I have a set of coordinates, which I would like to divide. I would like to have proper numbers with decimals, which doesn't happen in my worksheet, as I get messy data.
The first image shows the initial coordinates label. The second one shows the coordinates after split.
I need here the numbers with decimal.
I tried to divide them by the number, but it didn't work.
Sub Coordinatesfinal()
Columns("F:G").Insert Shift:=xlToRight
ActiveSheet.Range("E1").Value = "Latitude"
ActiveSheet.Range("F1").Value = "Longitude"
Dim rang As Range, cell As Range, rg As Range, element As Range, rg2 As Range
Dim r1 As Range, r2 As Range
Dim wors As Worksheet
Set wors = ActiveSheet
Dim myString As String
myString = "."
Dim LastRow As Long, i As Long, SecondLastRow As Long
LastRow = wors.Range("E" & wors.Rows.Count).End(xlUp).Row
Set rang = wors.Range("E2:E" & LastRow)
For Each cell In rang
cell = WorksheetFunction.Substitute(cell, ",", " ")
cell = WorksheetFunction.Substitute(cell, " ", " ")
cell = WorksheetFunction.Substitute(cell, ",,", " ")
Next
Set rg = [E2]
Set rg = Range(rg, Cells(Rows.Count, rg.Column).End(xlUp))
rg.TextToColumns Destination:=rg, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
If InStr(myString, ".") > 0 Then
Exit Sub
End If
With words
LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
SecondLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
End With
For Each element In wors.Range("E2:E" & LastRow)
cell.Value = cell.Value / 1000000
Next
For i = 2 To LastRow
Set r1 = Range("E" & i)
Set r2 = Range("F" & i)
If r1.Value > 54.5 Or r1.Value < 50 Then r1.Interior.Color = vbYellow
If r2.Value > 2 Or r2.Value < -7 Then r2.Interior.Color = vbCyan
'If r1.Value = 3 Then r2.Interior.Color = vbYellow
Next i
rg.TextToColumns Destination:=rg, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
MsgBox ("Coordinates prepared successfully")
End Sub
The
For Each element In wors.Range("E2:E" & LastRow)
cell.Value = cell.Value / 1000000
Next
VBA: Macro to divide range by a million
doesn't work at all, as same as:
For Each element In wors.Range("F2:F" & SecondLastRow)
If IsNumeric(element.Value) Then
If Len(element.Value) > 7 And Len(element.Value) < 9 Then
element.Value = element.Value / 1000000
ElseIf Len(element.Value) < 8 Then
element.Value = element.Value / 100000
Else
element.Value = element.Value / 10000000
End If
End If
Next
I don't know where might be the problem. As I might have several cases based on my total string length I would like to ask about some possibility of insert the "." symbol after 2nd character in my strings.
I tested this function:
Excel/VBA - How to insert a character in a string every N characters
but without any result.
Is there any way to divide these numbers or simply insert the "." symbol after 2nd number?
This is an output I would like to have


