2012-02-26 17 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. 

各セルには2つ以上の単語が含まれています。例:セルA1は「ロードハウスブルース」で構成されています。私はそのセルに存在する場合、大文字に変えることだけを 'roadh'にしたい。これはVBAで可能ですか?私があなただったら、私はminitechの皮肉な発言@耳を傾けるだろう

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. 

+2

「If」ステートメントでアルファベット全体をテストしていますか?慎重に、それは簡単にデイリーWTFに終わる可能性があります... – Ryan

+1

@minitechはい、私はです。ええ、私はこれが正しい方法ではないことを完全に知っていますが、もし私が1つの条件を持っていれば、これが可能かどうかを知りたがっています。 – user823911

+0

あなたは 'Like '* road [a-z] *" '... quickerを使うことができます –

答えて

3

これは、トリックを行います。あなたが探しているもの?が手紙a-zあるroad?が、その後a-zではなく、手動で全体のアルファベットを入力するためLikeを見てみましょうされている場合は...ここで

は、私はそれを行うだろうかです:

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。私は特にコンクールが好きです。検索する文字列を変更してその長さを変更するのを忘れるのは簡単です。私は、そのタイプの検索を処理するために私のユーティリティライブラリにPrefixを作成しました。 –

+0

+1私ができなかったものを見るため。もし作者が実際にまだ不明なものを実際に意味しているならば) –

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/

FIND/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はroadhを探しているだけでなく、どんな道路を探していますか?どこ?文字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'だけでなく' road? 'の'? 'が文字' az' 。 '?'が何であるか把握して大文字にする必要があります。それはこの問題の(やや)面白いひねりです。 –

+0

@ Jean-FrançoisCorbett:あなたは正しいかもしれないJCM :)。私はその質問を再読し、質問はあなたが単に暗示したことを意味するものではありません。または、私はあなたができることを見て行方不明ですか?これは作者が "セルA1は"ロードハウスのブルース "から成っていると言っています。 –

+0

さて、生意気ではありませんが、再び読んでください:OPは言う:**例:**セルA1は "ロードハウスブルース"で構成されています。 「Roadhouse Blues」の「roadh」は、ほんの一例であり、一般的に* OPが望むものを代表するものではありません。例の前に書かれているものを見てください。コードスニペットは、OPが単に「roadh」を探しているだけではないことを明確に示しています(これは質問のコメントにも暗示されています)。確かに、OPはこれについてはあまり明確ではない。 –

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 
関連する問題