2016-10-18 17 views
1

文字列内の単語を置き換えようとしています。以下のコードは、置き換えの仕事をしますが、私がしたくない部分一致も置き換えます。VBA内の文字列内の部分一致でない単語全体を検索するには

このコードはnorthをNに置き換えますが、これは素晴らしいですが、NorthernはNernを置き換えます。これは望ましくありません。どのように単語全体を比較するだけですか?

PHPでは==ですが、VBAではMS Access VBAでこれを使用しています。 WordMatch =:

答えて

0

あなたはここで

If InStr(inputString, "North") Then 
    inputString = inputString & " " '<-- add a space at the end to catch "North" should it be the last "word" in a possible "...North" string 
    inputString = Replace(inputString, " North ", " North ") '<-- double spaces before and after any " North " occurrence, necessary for subsequent statement to properly work should there be more than one " North " occurrence 
    inputString = Replace(inputString, " North ", " N") '<-- make the "core" replacement 
    inputString = Replace(Replace(inputString, " North ", " N"), " ", " ") '<-- make all double spaces occurrences as single ones 
End If 
+0

これはまさに私が欲しいものです、感謝の仲間 –

0

の "収縮" である。この

If InStr(inputString, "North") Then inputString = Trim(Replace(Replace(Replace(inputString & " ", " North ", " North "), " North ", " N"), " ", " ")) 

のように行くことができるあなたは

Public Function WordMatch(ByVal Text As String, ByVal Word As String) As Boolean 
Dim RightChar As String 
Dim LeftChar As String 
Dim IStart As Integer 
Dim IEnd As Integer 
Dim Flag As Boolean 
Dim Alphabet As String 
Alphabet = "abcdefghijklmnopqrstuvwxyz" 
Flag = True 

IStart = InStr(Text, Word) 

If Not (IStart > 0) Then 
    Flag = False 
Else 
    IEnd = IStart + Len(Word) - 1 

    If (IStart = 1 And IEnd = 1) Then GoTo WordMatched 

     If IStart > 1 Then 
      LeftChar = Mid(Text, IStart - 1, 1) 
      'LeftChar = Mid(Text, IStart - 1 - 3, 4) 
      'MsgBox "L'" & LeftChar & "' - " & Text & " - " & Word 
      If InStr(Alphabet, LeftChar) > 0 Then 
       Flag = False 
      End If 
     End If 
     If (IEnd < Len(Text)) Then 
      RightChar = Mid(Text, IEnd + 1, 1) 
      'RightChar = Mid(Text, IEnd + 1, 4) 
      'MsgBox "R'" & RightChar & "' - " & Text & " - " & Word 
      If InStr(Alphabet, RightChar) > 0 Then 
       Flag = False 
      End If 
     End If 

    'End If 

End If 

がWordMatched尋ねを解決しますフラグ 終了機能

関連する問題