Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA Selection.replace不替换所有内容_Vba_Excel - Fatal编程技术网

Excel VBA Selection.replace不替换所有内容

Excel VBA Selection.replace不替换所有内容,vba,excel,Vba,Excel,基本上,我试图做的是用VBA代码模拟替换按钮。我使用以下代码: Private Sub CommandButton1_Click() Blad1.Activate Blad1.Cells.Select Selection.Replace What:=".", Replacement:=",", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _

基本上,我试图做的是用VBA代码模拟替换按钮。我使用以下代码:

Private Sub CommandButton1_Click()
    Blad1.Activate
    Blad1.Cells.Select
    Selection.Replace What:=".", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub
这将替换所有的点,但在某些情况下,它会用空格而不是逗号替换点。为什么会这样?

试试这个

Sub Sample()
    Dim oRange As Range, aCell As Range, bCell As Range
    Dim ws As Worksheet
    Dim ExitLoop As Boolean
    Dim SearchString As String, FoundAt As String
    On Error GoTo Err
    Set ws = Blad1
    Set oRange = ws.Cells

    oRange.NumberFormat = "@"

    SearchString = "."
    Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    If Not aCell Is Nothing Then
        Set bCell = aCell
        Do While InStr(1, aCell.Formula, ".") > 0
            aCell.Formula = Replace(aCell.Formula, ".", ",")
        Loop
        Do While ExitLoop = False
            Set aCell = oRange.FindNext(After:=aCell)

            If Not aCell Is Nothing Then
                If aCell.Address = bCell.Address Then Exit Do
                Do While InStr(1, aCell.Formula, ".") > 0
                    aCell.Formula = Replace(aCell.Formula, ".", ",")
                Loop
            Else
                ExitLoop = True
            End If
        Loop
    End If
    Exit Sub
Err:
    MsgBox Err.Description
End Sub

这对我来说很好。你能举一个它不起作用的例子吗?嗯,我有很多数据,它在第一列中工作得很好,但当我进入第V列和第W列时,它跳过了很多单元格。我能在示例文件中看到第V/W列数据吗?当然。我也可以通过stackoverflow发送给你吗?或者我需要你的电子邮件吗?将文件上传到www.wikisend.com并在此处共享链接。谢谢,这确实有效。我猜是千位分隔符造成的,在仔细查看结果后,我发现只有多于4个数字的单元格比较麻烦。