2016-04-04 13 views
0

マクロVBAで新しく問題が発生しました。マクロVBA - 両方の文字列と似た番号の比較

私は比較する2つの文字列を持っていますが、どの文字列を両方の文字列に見つかった類似性の数字が表示されている結果として表示するのですか?

文字列1:1,2,3,4,6,7,8,9,10,11,12,13,19,20

文字列2:2,3,7,8,9 、10,11

比較後:

結果:2,3,7,8,9,10,11

コード:

If ActiveSheet.Cells(irow + 1, 12).Value = "" Then 

    'MsgBox "Data not found" 
Else 
    temp = vbNullString 
    temp = ActiveSheet.Cells(irow + 1, 12).Value 
    'expanddata() use to expend a sequence of numbers into a display string as below 
    ' 1,2-4,6 -> 1,2,3,4,6 
    temp = expanddata(temp) 

    If Worksheets("AI").Cells(irow + 1, 10).Value = temp Then 
     temp = ConvNum(temp) 'if whole string same then convert back to 1,2-4,6 
    Else 
     'the comparision make in here   
    End If 
Worksheets("AI").Cells(irow + 1, 10) = temp 

End If 

はありがとうございます。

+1

あなたは以上のループ、その後、二つの配列を作成するために、各文字列に 'スプリット(stringHereを、「」)'を使用することができます配列を比較し、内容を比較します。 –

+0

Tim Williamsさん、ありがとう、私はすでに問題を解決しています。どうもありがとう。 :) – Empty

+1

この場合、質問を削除するか、解答として回答を投稿すると便利です。 –

答えて

0
For irow = 1 To numofrow 
     ptcolno = 12 
     If ActiveSheet.Cells(irow + 1, 12).Value = "" Then 
      'MsgBox "Data not found" 
     Else 
      temp = vbNullString 
      temp = ActiveSheet.Cells(irow + 1, 12).Value 
      temp = expanddata(temp) 

      If Worksheets("AI").Cells(irow + 1, 10).Value = temp Then 
       temp = ConvNum(temp) 
      Else 
       ' Answer 
       Temp2 = Worksheets("AI").Cells(irow + 1, 10).Value 
       arr1 = Split(Temp2, ",") 
       arr2 = Split(temp, ",") 

       temp = vbNullString 
       For i = LBound(arr2) To UBound(arr2) 
        For j = LBound(arr1) To UBound(arr1) 
         If arr2(i) = arr1(j) Then 
          temp = temp & "," & arr2(i) 
         End If 
        Next j 
       Next i 
       temp = Right(temp, Len(temp) - 1) 
       temp = ConvNum(temp) 
       ' End 
      End If 
      Worksheets(checktype & "_BUYOFF_1").Cells(irow + 1, 68) = temp 
0

次のコードを試してください。

Sub comparestring() 
    string1 = "1,2,3,4,6,7,8,9,10,11,12,13,19,20" 
    string2 = "2,3,7,8,9,10,11" 
    str1 = Split(string1, ",") 
    str2 = Split(string2, ",") 
    For i = 0 To UBound(str1) 
     For j = 0 To UBound(str2) 
      If str1(i) = str2(j) Then 
       If matchedcontent <> "" Then 
        matchedcontent = matchedcontent & "," & str1(i) 
       Else 
        matchedcontent = str1(i) 
       End If 
      End If 
     Next j 
    Next i 
    Range("A3").Value = matchedcontent 
End Sub 

結果以下のような文字列1および列2に2つの文字列がセルA3

string1=Activesheet.Range("A1").Value 
string2=Activesheet.Range("A2").Value 
+0

こんにちは、問題は解決され、私もこれを試してみます。ありがとうございました。 :) – Empty

0

を以下のように呼ぶことができ、この

Option Explicit 

Function CompareStrings(string1 As String, string2 As String) As String 
Dim s As Variant 

For Each s In Split(string1, ",") 
    If "," & string2 & "," Like "*," & s & ",*" Then CompareStrings = CompareStrings & s & "," 
Next s 

CompareStrings = Left(CompareStrings, Len(CompareStrings) - 1) 
End Function 

を試みるで印刷する割り当て

Sub main() 

Dim string1 As String, string2 As String, stringRes As String 

string1 = "1,2,3,4,6,7,8,9,10,11,12,13,19,20" 
string2 = "2,3,7,8,9,10,11" 

stringRes = CompareStrings(string1, string2) 

MsgBox stringRes 
End Sub 
+0

こんにちは、問題は解決され、私もこれを試してみます。ありがとうございました。 :) – Empty

1

pを自動化するowershellテキストファイルCにリストを印刷します:\一時\ test.txtの

Sub Test() 
a = "(1,2,3,4,6,7,8,9,10,11,12,13,19,20)" 
b = "(2,3,7,8,9,10,11)" 
cmd = Shell("powershell.exe """ & a & """ | Where {""" & b & """ -Contains $_} | out-file c:\temp\test.txt", 1) 
End Sub 
+0

こんにちは、私はそれを試してみる、ありがとう。 – Empty