2017-11-07 7 views
0

2つの文字列を比較して一致する文字列を比較しようとしています。 コードは多くのファイルを開き、各ファイルに対して、 が私が定義したものとおそらく一致する2つの文字列をフェッチします。 しかし、セル内の文字列の1つにキャリッジリターンが含まれています。これは なので、一致しないように見えます。 本当にありがとうございます。 ありがとうございます。VBA文字列返品送料との比較

比較のための私のコードの関連部分は以下の通りである:

Dim i As Integer 
Dim a, WScount, rows, j As Integer 
Dim temp As String 
Dim check1, check2 As Boolean 
Dim str1, str2 As String 

i = 1 
j = 1 

str1 = "Manufacturers Number" 
str2 = "Manufacturers" & vbCrLf & " Number" 

For Each WS In Worksheets 
    N = 0 
    rows = 1 
    While N < 7 
     temp = src.ActiveSheet.Cells(rows, 2) 
     check1 = StrComp(str1, temp, vbTextCompare) 
     check2 = StrComp(str2, temp, vbTextCompare) 
     If check1 = 0 Or check2 = 0 Then 
      For k = rows + 1 To 100 
       temp = Cells(k, 2) 
       If Not StrComp("", temp, vbTextCompare) = False Then 
        ThisWorkbook.Worksheets(1).Cells(j, 3) = temp 
        j = j + 1 
       End If 
      Next k 
     End If 
      rows = rows + 1 
      N = N + 1 
    Wend 

Next WS 
+2

キャリッジリターンを 'Dim strCutted As String = Replace(str2、vbCrLf、" ")のような空文字列に置き換えることができます。 –

答えて

0

vbCrLfは例えばasc 10及び13に等しいです。 "改行"と "キャリッジリターン"。 Ascii Table

Option Explicit 

Public Sub TestMe() 

    Dim str1 As String 
    Dim str2 As String 

    str1 = "Manufacturers Number" 
    str2 = "Manufacturers" & vbCrLf & " Number" 

    str1 = stripTheString(str1) 
    str2 = stripTheString(str2) 

    Debug.Print str1 = str2 

End Sub 

Public Function stripTheString(strToStrip As String) As String 

    Dim cnt  As Long 
    Dim ascVal As Long 
    Dim newStr As String 

    For cnt = 1 To Len(strToStrip) 
     ascVal = Asc(Mid(strToStrip, cnt, 1)) 
     If Not (ascVal = 10 Or ascVal = 13) Then 
      newStr = newStr & Mid(strToStrip, cnt, 1) 
     End If 
    Next cnt 

    stripTheString = newStr 

End Function 

stripTheString機能new linecarriage returnをチェックしたコードからそれらを削除します。

したがって、あなたでし単にこれらの2つの値とは、それを試してみます。上のコードは少し問題があるかもしれません。それに続く改行がない場合は、stripTheString関数の新しい行が消去される限り問題あります。

+1

Thanks Vityata !! :)それは働いた –

+0

@ JasserBenYounes - ようこそ。 – Vityata