2

我有一个定期收到的电子表格,其中包含大量包含名称的单元格。一些单元格有一个全名,包括带句点的中间首字母。

例如:

斯普林格,杰里 A.

尽管我收到的表格时不时地会有以下内容:

斯普林格,杰瑞。

我需要摆脱那些中间的首字母,但还要检查以确保我只是删除了“。” 如果它在那里。

请原谅我缺乏正确的逻辑,但我有以下 ca-ca 子:

Sub DeleteMiddleI()
Dim nr1, nr2 As Range
Dim col As Integer

col = 1
Set nr1 = Cells(65536, col).End(xlUp)
Set nr2 = Cells(col, 1)

Do While nr2 <> nr1
    'Check to be sure the cell isn't empty
    If Len(nr2) <> 0 Then
         'Check to see if the last character is a "."
         If Right$(nr2, 1) = "." Then
            'Check and be sure there is a character before the "."
            If InStr(1, nr2.Text, "[A-Z].") > 0 Then '<<<<<<CODE BREAKAGE
                nr2 = Left$(nr2, Len(nr2) - 3)
            End If
         End If
    End If

    col = col + 1
    Set nr2 = Cells(col, 1)
Loop

End Sub

它打破了

如果 InStr(1, nr2.Text, "[AZ].") > 0 那么

我觉得自己很愚蠢……但我错过了什么?

4

2 回答 2

4

这会有帮助吗?这将取代所有的“。” 一无所有。

Option Explicit

Sub Sample()
    Sheets("Sheet1").Cells.Replace What:=" .", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub

编辑

删除首字母部分是什么意思?你的意思是Springer, Jerry A.改成Springer, Jerry还是Springer, Jerry .改成Springer, Jerry

如果是,那么这会有帮助吗?

Option Explicit

Sub Sample()
    Dim LastRow As Long
    Dim Pos As Long, i As Long

    With Sheets("Sheet1")
        LastRow = .Range("A" & Rows.Count).End(xlUp).Row

        For i = 1 To LastRow
            Pos = InStr(1, .Range("A" & i).Value, ", ") + 2
            If Pos > 2 Then
                Pos = InStr(Pos, .Range("A" & i).Value, " ")
                If Pos > 0 Then .Range("A" & i).Value = Mid(.Range("A" & i).Value, 1, Pos - 1)
            End If
        Next i
    End With
End Sub
于 2012-05-04T18:07:16.800 回答
0

Could you change the if test from:

If Right$(nr2, 1) = "." Then

to

If (Right$(nr2, 1) = "." AND Left$(nr2, 1) <> "." ) Then
于 2012-05-04T18:12:09.610 回答