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

String 在excel vba中删除给定索引处字符串的元素,string,vba,excel,formatting,String,Vba,Excel,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 所

如何在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语句中,我想编写代码,删除截止指定索引处的句点。我该怎么做


提前谢谢

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

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

如果您只想删除所有句点,您可以使用
Replace()
-如果您只想删除特定的句点,那么您可以使用Left()和Right()获取特定索引前后的字符串部分,并将它们连接起来。这很有效,谢谢@您可以使用
Replace
Start
Count
参数仅替换某些实例。(
Replace(string,“.”,“”,1,1,vbTextCompare)
BTW您应该将此作为答案发布另外,对于14k单元格,您可能希望使用VBA数组而不是工作表,除非您告诉我在您的计算机上只需不到一秒钟的时间,这可能是事实,但我仍然不喜欢它^^