Excel 同时使用replace和find函数查找几个;点“;价值观

Excel 同时使用replace和find函数查找几个;点“;价值观,excel,vba,Excel,Vba,我使用查找和替换功能查找数字中选定单元格范围内的点,并将其替换为零 我想实现一个代码来查找多个点,并通过只实现一次这个过程来替换它们。例如: 选择:1.169.499,08-->所需输出:1169499,08 选择:111.222,08-->所需输出:111222,08 我尝试的代码是: Sub DEtoFR() 'defining the variable z which stores the German number formatting 'defining the variable x

我使用查找和替换功能查找数字中选定单元格范围内的点,并将其替换为零

我想实现一个代码来查找多个点,并通过只实现一次这个过程来替换它们。例如:

选择:1.169.499,08-->所需输出:1169499,08

选择:111.222,08-->所需输出:111222,08

我尝试的代码是:

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

您可以使用
Find
FindNext
来执行此操作,而不是循环

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
关于效率的评论后更新 我使用以下方法生成了1000个带小数的随机数:

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
然后设置两个测试。第一个命名为
FindNextReplace
(我的方法)和第二个
RangeReplace
@JvdV方法

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

然后我添加了一个计时器函数,我可以从中调用这两个函数

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
我使用
CreateDecimals
生成了随机数,然后复制了它们,这样我就可以在两个测试中使用相同的值。我运行了一个,替换了
TimerTest
sub中的子名称,并替换了
Replace
之前的原始值,然后再次运行它

结果:


正如您所看到的,@JvdV方法显然更有效

这里是另一种做事方式,也许您会学到一些有用的东西:

之前:

代码:

结果:


如果值中确实有句号,您只需:
x.value=Replace(x.value,“,”)
非常感谢您的正确回答。我刚刚将“set z=Sheet1.Range(“A1:A10”)替换为“set z=Selection.SpecialCell(xlCellTypeConstants,xlCellTypeConstants)”。一切都很完美,但我在输出中得到了错误编号格式。你能调整你的代码以避免输出中的错误编号格式吗?@Tom,会使用z。替换什么:=”,替换:=”,搜索顺序:=xlByColumns,匹配案例:=True适合你吗?@JvdV-yup刚刚测试过,它会-不知道你能处理整个范围。更喜欢你的答案而不是我的答案,所以你的答案会在几分钟内删除我的答案mins@JvdV好吧,我不说了,但与其他方法相比,这可能是一种低效的方法yours@JvdV-查看我的更新。只是用1000个值对两者进行了比较。到目前为止,你的方法显然更有效
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
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