Andrewz,其中一些答案确实很优雅,但你提出的问题是否正确?
作为一名学生,我在因斯布鲁克的 Schneeburggasse 街上度过了美好的一年。尽管我的邻居很令人愉快,但我相信他们会在他们的街道变成 Schneeburggaße 时嗤之以鼻。同样,我的德国笔友曾经住在一条叫做 Schloßstraße 的道路上——如果在您的数据库中记录为 Schlossstrasse,那么 Schlossstraße 看起来不会有点奇怪吗?
我的意思是,仅仅替换最后一个 ss 可能会给你一些非常奇怪的结果。如果没有编写一个极其复杂的语素分析程序来应用已经很脆弱的 Eszett 规则,您将需要一个更可靠的解决方法。
我建议创建一个常用名称的集合,例如 Straße、Schloß 等,您肯定需要替换它们。对它们运行替换,然后存储任何其他出现的 ss 供您循环并手动检查。类似于下面的代码:
Option Explicit
Private mCommonWords As Collection
Private mAmbiguous As Collection
Public Sub RunMe()
Dim str As String
Dim cell As Range
CreateCommonWordList
ReplaceOrNote
' Do anything you like with the list of ambiguous cells
For Each cell In mAmbiguous
str = str & cell.Address(False, False) & vbLf
Next
MsgBox str
End Sub
Private Sub CreateCommonWordList()
Set mCommonWords = New Collection
AddCommonWord "straße", "strasse"
AddCommonWord "straße", "str."
AddCommonWord "schloß", "schloss"
End Sub
Private Sub AddCommonWord(correct As String, wrong As String, Optional capitalise As Boolean = True)
Dim words(1) As String
Dim splitCorrect(1) As String
Dim splitWrong(1) As String
words(0) = correct
words(1) = wrong
mCommonWords.Add words
If capitalise Then
splitCorrect(0) = UCase(Left(correct, 1))
splitCorrect(1) = Mid(correct, 2, Len(correct) - 1)
correct = splitCorrect(0) & splitCorrect(1)
splitWrong(0) = UCase(Left(wrong, 1))
splitWrong(1) = Mid(wrong, 2, Len(wrong) - 1)
wrong = splitWrong(0) & splitWrong(1)
words(0) = correct
words(1) = wrong
mCommonWords.Add words
End If
End Sub
Private Sub ReplaceOrNote()
Dim ws As Worksheet
Dim v As Variant
Dim startCell As Range
Dim foundCell As Range
Set ws = ThisWorkbook.Worksheets("Sheet1")
' First replace the common words
For Each v In mCommonWords
ws.Cells.Replace _
What:=v(1), _
Replacement:=v(0), _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=True, _
SearchFormat:=False, _
ReplaceFormat:=False
Next
' Now search for every other 'ss' member
Set mAmbiguous = New Collection
Set startCell = ws.Cells.Find( _
What:="ss", _
After:=ws.Cells(ws.Rows.Count, ws.Columns.Count), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True)
If Not startCell Is Nothing Then
mAmbiguous.Add startCell
Set foundCell = startCell
Do
Set foundCell = ws.Cells.FindNext(foundCell)
If foundCell Is Nothing Then
Exit Do
ElseIf foundCell.Address = startCell.Address Then
Exit Do
Else
mAmbiguous.Add foundCell
End If
Loop While True
End If
End Sub
您好 Ambie,我知道因斯布鲁克很漂亮……您的代码也是如此。我的问题是我必须加载街道地址、邮政编码等 Webfleet。这是一个用于跟踪服务车(地理定位)的在线门户(德语)。如果我在司机终端 TomTom 8275 上上传每日服务之旅,那么如果街道名称以 strasse 结尾,则执行该操作的 excel 工具通常会报告错误(在地理编码上)。 excel工作表中许多地址行的另一个问题以str结尾。 (因斯布鲁克街)。所以我必须把它换成 Insbruckerstraße。我已经测试了你的代码,他解决了这两个问题。但在 Strasserstr。我认为他将其更改为 Straßerstraße 是因为字母系列 strasse 在 strasser 中。好的,我可以忍受...再次感谢