Home » excel » excel – Using replace and find function together to look for several "dot" values at the same time

excel – Using replace and find function together to look for several "dot" values at the same time

Posted by: admin May 14, 2020 Leave a comment

Questions:

I use FIND and REPLACE function to look for dots in a range of selected cells in the number and replace them with nothing.

I want to implement a Code to look for more than one dots and replace them by implementing this procedure only once. For example:

Selection: 1.169.499,08 –> desired Output: 1169499,08

Selection: 111.222,08 –> desired Output: 111222,08

What I have tried as a Code is:

Sub DEtoFR()
'defining the variable z which stores the German number formatting
'defining the variable x which stories the French number formatting

Dim z as Range, x as Variant
Set z = Selection.SpecialCells(xlCellTypeConstants,xlCellTypeConstants)

'Find Counts the Location of the "." character.
'Replace will look for it and replace "." with "". 

For Each x in z
    x.Value = Application.WorksheetFunction.Replace(x.Value, Application.WorksheetFunction.Find(".", x.value), 1, "")
Next x

End Sub
How to&Answers:

Here is another way of doing things, maybe you’ll pick up something usefull:

Before:

enter image description here

Code:

Sub Test()

Dim RNG As Range, LR As Double
With ActiveWorkbook.Sheets(1)
    LR = .Cells(Rows.Count, 1).End(xlUp).Row
    Set RNG = .Range(Cells(1, 1), Cells(LR, 1)).SpecialCells(2)
    RNG.Replace What:=".", Replacement:="", SearchOrder:=xlByColumns, MatchCase:=True
End With

End Sub

Result:

enter image description here

Answer:

You could use Find and FindNext to do this instead of looping

Sub demo()
    Dim z As Range, c As Range
    ' Declare you range
    Set z = Sheet1.Range("A1:A10")

    With z
        Set c = .Find(".")

        If Not c Is Nothing Then
            Do
                c.Replace what:=".", replacement:=vbNullString
                Set c = .FindNext(c)
            Loop Until c Is Nothing
        End If
    End With
End Sub

Update after comments about efficiency
I generated 1000 random numbers with decimals using:

Sub CreateDecimals()
    Dim c As Range
    For Each c In Sheet1.Range("A1:A1000")
        c.Value2 = WorksheetFunction.RandBetween(0, 500000) / 100
    Next c
End Sub

Then set up two tests. The first named FindNextReplace (my method) and the second RangeReplace @JvdV method.

Public Sub FindNextReplace()
    Dim c As Range
    With Sheet1.Range("A1:A1000")
        Set c = .Find(".")

        If Not c Is Nothing Then
            Do
                c.Replace what:=".", replacement:=vbNullString
                Set c = .FindNext(c)
            Loop Until c Is Nothing
        End If
    End With
End Sub

Public Sub RangeReplace()
    With Sheet1.Range("A1:A1000")
        .Replace what:=".", replacement:=vbNullString, searchorder:=xlByColumns, MatchCase:=True
    End With
End Sub

I then added a timer function that I could call both from

Sub TimerTest()
    Dim StartTime As Double
    Dim SecondsElapsed As Double

    StartTime = Timer
    Call RangeReplace

    SecondsElapsed = Round(Timer - StartTime, 2)

    Debug.Print "RangeReplace took:", SecondsElapsed
End Sub

I generated the random numbers using CreateDecimals then took a copy of them so I could use the same values for both tests. I ran one, replaced the sub names in the TimerTest sub and replaced the original values before the Replace and ran it again.

Results:

Results

As you can see @JvdV method is clearly more efficient