Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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 仅在字符串中的特定位置将逗号(,)替换为点(.)_Excel_Vba - Fatal编程技术网

Excel 仅在字符串中的特定位置将逗号(,)替换为点(.)

Excel 仅在字符串中的特定位置将逗号(,)替换为点(.),excel,vba,Excel,Vba,我在多行上都有字符串,如下所示 S087A1097,99,86,0,14,0,good S087A1097,100,0,10,14,0,good S087A1097,0,0,100,0,0,good 我需要分别把它改成这个 S087A1097,99.86,0.14,0,good S087A1097,100.0,10.14,0,good S087A1097,0.0,100.0,0,good 如果您的文本位于单元格A1中,如何在Excel中实现这一点: =SUBSTITUTE(SUBSTITUT

我在多行上都有字符串,如下所示

S087A1097,99,86,0,14,0,good
S087A1097,100,0,10,14,0,good
S087A1097,0,0,100,0,0,good
我需要分别把它改成这个

S087A1097,99.86,0.14,0,good
S087A1097,100.0,10.14,0,good
S087A1097,0.0,100.0,0,good

如果您的文本位于单元格A1中,如何在Excel中实现这一点:

=SUBSTITUTE(SUBSTITUTE(A1,",",".",2),",",".",3)

如果文本位于单元格A1中:

=SUBSTITUTE(SUBSTITUTE(A1,",",".",2),",",".",3)

如果要使用VBA解决方案,可以尝试以下代码

它可能看起来有点长,但执行起来非常快,因为工作表几乎没有“混乱”,而且大部分逻辑都是在数组上完成的

代码

Option Explicit

Sub ImportCsvContents()

Dim csvLines As Variant, CurrentRow As Variant
Dim LastRow As Long, i As Long

ReDim csvLines(0)

With Worksheets(1)
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    ' === logic, each order ends with data data in column "B" ===
    For i = 1 To LastRow
        csvLines(UBound(csvLines)) = .Range("A" & i).Value
        ReDim Preserve csvLines(UBound(csvLines) + 1) ' keep record and raise array index by 1
    Next i
End With

' resize array to actual populated size
If csvLines(UBound(csvLines)) = "" Then
    ReDim Preserve csvLines((UBound(csvLines) - 1))
End If


' loop through all lines in .csv file
For i = 0 To UBound(csvLines)
    CurrentRow = Split(csvLines(i), ",")

    CurrentRow(1) = CurrentRow(1) & "." & CurrentRow(2)
    CurrentRow(2) = CurrentRow(3) & "." & CurrentRow(4)
    CurrentRow(3) = CurrentRow(5)
    CurrentRow(4) = CurrentRow(6)

    ' re-format the current line
    csvLines(i) = CurrentRow(0) & "," & CurrentRow(1) & "," & CurrentRow(2) & "," & CurrentRow(3) & "," & CurrentRow(4)

    Erase CurrentRow ' clear array
Next i

' now just dump the entre array to the worksheet
Worksheets(1).Range("A1").Resize(UBound(csvLines) + 1).Value = Application.Transpose(csvLines)

End Sub

如果要使用VBA解决方案,可以尝试以下代码

它可能看起来有点长,但执行起来非常快,因为工作表几乎没有“混乱”,而且大部分逻辑都是在数组上完成的

代码

Option Explicit

Sub ImportCsvContents()

Dim csvLines As Variant, CurrentRow As Variant
Dim LastRow As Long, i As Long

ReDim csvLines(0)

With Worksheets(1)
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    ' === logic, each order ends with data data in column "B" ===
    For i = 1 To LastRow
        csvLines(UBound(csvLines)) = .Range("A" & i).Value
        ReDim Preserve csvLines(UBound(csvLines) + 1) ' keep record and raise array index by 1
    Next i
End With

' resize array to actual populated size
If csvLines(UBound(csvLines)) = "" Then
    ReDim Preserve csvLines((UBound(csvLines) - 1))
End If


' loop through all lines in .csv file
For i = 0 To UBound(csvLines)
    CurrentRow = Split(csvLines(i), ",")

    CurrentRow(1) = CurrentRow(1) & "." & CurrentRow(2)
    CurrentRow(2) = CurrentRow(3) & "." & CurrentRow(4)
    CurrentRow(3) = CurrentRow(5)
    CurrentRow(4) = CurrentRow(6)

    ' re-format the current line
    csvLines(i) = CurrentRow(0) & "," & CurrentRow(1) & "," & CurrentRow(2) & "," & CurrentRow(3) & "," & CurrentRow(4)

    Erase CurrentRow ' clear array
Next i

' now just dump the entre array to the worksheet
Worksheets(1).Range("A1").Resize(UBound(csvLines) + 1).Value = Application.Transpose(csvLines)

End Sub

很快,很好:)谢谢!我必须转换我的语言环境;对,这是最让人困惑的部分。。。整件事都是逗号、点和引号:)你的真是令人印象深刻!谢谢,我的更长,但如果你不想在工作表中保留公式,这是一个快速解决方案非常快,很好:)谢谢!我必须转换我的语言环境;对,这是最让人困惑的部分。。。整件事都是逗号、点和引号:)你的真是令人印象深刻!谢谢,我的答案更长,但如果你不想在工作表中保留公式,这是一个快速解决方案如果你想使用基于VBA的解决方案,你可以阅读下面的答案和代码如果你想使用基于VBA的解决方案,你可以阅读下面的答案和代码