We help IT Professionals succeed at work.
Get Started

VBA Replace Function - What am I missing?

Elena Quinn
Elena Quinn asked
Last Modified: 2017-04-24
Hi All,

I suspect this would be fairly simple, but I am missing it.  I am simply trying to take a number that is formatted in a time format and changing them all to seconds, so I can work with the same units.  The original value is formatted with decimals, like 1.234.567.890, and it will be designated at (ns), (us), (ms), etc.  So the first thing I need to do is replace all except the first decimal.  I am trying to use the replace function for that.  But it is replacing all, despite the starting number I am entering.
Dim myArray() As Variant
Dim NumText As String
Dim DecPos As Integer
Dim i as long

i = 0
ReDim myArray(Selection.Count)

For Each cel In Selection
    myArray(i) = cel.Value
    i = i + 1

For i = LBound(myArray) To UBound(myArray)
NumText = myArray(i)
DecPos = InStr(1, NumText, ".")
If DecPos > 0 Then
    If InStr(1, (Right(NumText, Len(NumText) - DecPos)), ".") > 0 Then
        NumText = Replace(NumText, ".", "", Start:=DecPos + 1)
    End If
End If

Open in new window

So NumText starts as '1.412.877.593 (s)', but after the replace function, it is '412877593 (s)'.  What happened to the "1." at the beginning?

Watch Question
Analyst Assistant
Distinguished Expert 2020
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE