在excel vba中删除给定索引处的字符串元素

时间:2014-10-16 20:24:55

标签: string excel vba excel-vba formatting

如何在excel vba中删除给定索引处的字符串元素?我目前的代码如下:

Private Sub RemovePeriods_Click()
Dim i As Integer
Dim Cutoff As Integer

For i = 0 To 14283

Cutoff = InStr(1, Range("K6").Offset(i, 0).Value, ".", vbTextCompare)
If Cutoff > 0 Then

End If
Next i
End Sub

因此,在最后一个if语句中,我想编写一个代码来删除cutoff指定的索引处的句点。我该怎么做?

提前致谢!

1 个答案:

答案 0 :(得分:0)

这将满足您的要求。试试这个,让我知道。

Private Sub RemovePeriods_Click()

        Dim i               As Integer
        Dim Cutoff          As Integer
        Dim rngData         As Range
        Dim strToReplace    As String

        'Assign String to replace here
        strToReplace = "."

        For i = 0 To 14283
            'Assign the cell to Range
            Set rngData = Range("K6").Offset(i, 0)
            Cutoff = InStr(1, rngData, strToReplace, vbTextCompare)
            If Cutoff > 0 Then
                'Apply Replace formula
                rngData = WorksheetFunction.Replace(rngData, Cutoff, Len(strToReplace), "")
            End If
        Next i

        Set rngData = Nothing

End Sub