這是另一種方式。讓Excel做骯髒的工作;的)
Sub Sample()
Dim SearchString As String
Dim ReplaceString As String
Dim aCell As Range
'~~> Search String
SearchString = "roadh"
'~~> Replace string
ReplaceString = UCase(SearchString)
'~~> Change A1 to to the respective cell
Set aCell = Range("A1").Find(What:=SearchString, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
Range("A1").Replace What:=SearchString, Replacement:=ReplaceString, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End If
End Sub
而且代替循環,你可能想使用.FIND/.FINDNEXT?
更多關於 '查找/ FindNext中':http://siddharthrout.wordpress.com/2011/07/14/find-and-findnext-in-excel-vba/
查找/ FindNext中遠遠更爲更快然後循環和搜索在Excel單元格的值;)
及以下的還要快(實際上是最快的)。如果你的最終目的是要取代這個詞,你不需要找到這個詞。只需發出替換命令。如果代碼找到任何單詞,則會自動替換。
Sub Sample()
Dim SearchString As String
Dim ReplaceString As String
'~~> Search String
SearchString = "roadh"
'~~> Replace string
ReplaceString = UCase(SearchString)
'~~> Replace the range below with the respective range
Range("A1:A1000").Replace What:=SearchString, Replacement:=ReplaceString, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
您不需要使用通配符來檢查字符串中是否存在字符串。 xlPart在 「注視:= xlPart」 需要照顧的是:)
隨訪(萬一用戶意味着這個)
您可以在這裏失去了點...... OP不僅在尋找道路,而且在任何道路上尋找道路?在哪裏?是一個字母a-z。你必須弄清楚什麼?是,並使其成爲大寫。這是這個問題的(輕度)有趣的轉折。 - 讓弗朗索瓦科貝特1小時前
另外檢查其中細胞可以包含多個「路」的值(如圖快照低於該具有情形的「之前」和「之後」快照。
Sub Sample()
Dim oRange As Range, aCell As Range, bCell As Range
Dim ws As Worksheet
Dim ExitLoop As Boolean
Dim SearchString As String, FoundAt As String
On Error GoTo Whoa
Set ws = Worksheets("Sheet1")
Set oRange = ws.Columns(1)
SearchString = "road"
Set aCell = oRange.Find(What:=SearchString & "?", LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
FoundAt = aCell.Address
aCell.Value = repl(aCell.Value, SearchString)
Do While ExitLoop = False
Set aCell = oRange.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
FoundAt = FoundAt & ", " & aCell.Address
aCell.Value = repl(aCell.Value, SearchString)
Else
ExitLoop = True
End If
Loop
MsgBox "The Search String has been found these locations: " & FoundAt & " and replaced by UPPERCASE"
Else
MsgBox SearchString & " not Found"
End If
Exit Sub
Whoa:
MsgBox Err.Description
End Sub
Function repl(cellValue As String, srchString As String) As String
Dim pos As Integer
pos = InStr(1, cellValue, srchString, vbTextCompare)
repl = cellValue
Do While pos <> 0
If pos = 1 Then
repl = UCase(Left(repl, Len(srchString) + 1)) & Mid(repl, Len(srchString) + 2)
Else
repl = Mid(repl, 1, pos - 1) & UCase(Mid(repl, pos, Len(srchString) + 1)) & _
Mid(repl, pos + Len(srchString) + 1)
End If
Debug.Print repl
pos = InStr(pos + 1, repl, srchString, vbTextCompare)
Loop
End Function
快照:
HTH
希德
您是否在一個'If'語句中測試整個字母表?小心,這可能很容易結束在每日WTF ... – Ryan 2012-02-26 18:44:17
@minitech是的,我。是的,我完全知道這不是正確的方法,但只是想知道如果我有一個條件,這是否可能。 – user823911 2012-02-26 18:46:17
你可以使用'喜歡'*道路[a-z] *「'...更快 – 2012-02-27 12:43:24