2012-02-26 53 views
3

如何将特定字符串更改为只有大写字母存在。如果字符串存在,则将其更改为大写 - VBA

If (Cells(i, "A") Like "*roada*") Or (Cells(i, "A") Like "*roadb*") _ 
Or (Cells(i, "A") Like "*roadc*") etc... Then 'Change only the found string to Uppercase. 

每个单元包含两个或更多单词。示例:单元格A1包含“roadhouse blues”。如果它存在于该单元格中,我只需要'roadh'即可更改为大写字母。这在VBA中可能吗?

+2

您是否在一个'If'语句中测试整个字母表?小心,这可能很容易结束在每日WTF ... – Ryan 2012-02-26 18:44:17

+1

@minitech是的,我。是的,我完全知道这不是正确的方法,但只是想知道如果我有一个条件,这是否可能。 – user823911 2012-02-26 18:46:17

+0

你可以使用'喜欢'*道路[a-z] *“'...更快 – 2012-02-27 12:43:24

回答

3

这将这样的伎俩:

Const road As String = "road" 

Dim s As String 
Dim letterAfterRoad As String 

s = "play that roadhouse blues" ' or get contents of some cell 
letterAfterRoad = Mid(s, InStr(s, road) + Len(road), 1) 
Mid(s, InStr(s, road)) = UCase(road & letterAfterRoad) 

Debug.Print s ' returns "play that ROADHouse blues". Write to cell. 

如果我是你,我会听从@ MiniTech移动的一句讽刺的话。如果你正在寻找的是road?其中?是一个字母a-z然后让Likea-z而不是手动键入整个字母表......

这里是我会怎么做:

Const road As String = "road" 

Dim s As String 
Dim charAfterRoad As String 
Dim roadPos As Long 

s = "play that roadhouse blues" 

roadPos = InStr(s, road) 
If roadPos > 0 And Len(s) >= roadPos + Len(road) Then 
    'Found "road" and there is at least one char after it. 
    charAfterRoad = Mid(s, roadPos + Len(road), 1) 
    If charAfterRoad Like "[a-z]" Then 
     Mid(s, InStr(s, road)) = UCase(road & charAfterRoad) 
    End If 
End If 

Debug.Print s ' returns "play that ROADHouse blues" 
+1

+1。我特别喜欢道路上的Const。改变你搜索的字符串太容易了,忘记改变它的长度。我在我的实用程序库中创建了一个前缀来处理这种类型的搜索。 – 2012-02-26 19:54:41

+0

+1用于查看我无法做到的事情。即如果作者实际上意味着哪一个还不清楚:) – 2012-02-27 09:54:39

3

这是另一种方式。让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 

快照

enter image description here

HTH

希德

+0

您可能会错过这里的观点...... OP不仅仅在寻找'roadh',而是寻找任何'路?',其中'?'是一个字母'az' 。你必须弄清楚“?”是什么,并使其成为大写。这是这个问题的(轻度)有趣的转折。 – 2012-02-27 08:31:57

+0

@ Jean-FrançoisCorbett:你可能是对的JCM :)。我重新阅读这个问题,而这个问题并不意味着你刚才的暗示。或者我错过了看到什么,你可以?这就是作者所说的“单元格A1包含”roadhouse blues“,如果它存在于单元格中,我只想要'更改为'大写'。” – 2012-02-27 08:45:31

+0

好吧,不要厚颜无耻,但请再读一遍:OP说**例:**单元格A1由“roadhouse blues”组成。 “roadhouse blues”中的“roadh”事物*仅仅是一个例子*,并不代表OP希望*一般情况*。看看这个例子之前写的是什么。代码片段清楚地表明,OP不只是在寻找'roadh'(这也在问题的评论中暗示)。诚然,OP对这些都不是很清楚。 – 2012-02-27 09:17:50

1

用正则表达式的一种方式,替换所有路*输入。

Sub repl(value As String) 
    Dim re As Object: Set re = CreateObject("vbscript.regexp") 
    Dim matches As Object, i As Long 
    re.IgnoreCase = True 
    re.Global = True 
    re.Pattern = "(road[A-Z])" 
    Set matches = re.Execute(value) 
    For i = 0 To matches.Count - 1 
     value = Replace$(value, matches(i), UCase$(matches(i))) 
    Next 
    Debug.Print value 
End Sub 
相关问题