2016-04-04 107 views
0

我是Macro VBA中的新成員,並且遇到問題。Macro VBA - 比較兩個字符串中的相似數字

我有兩個字符串進行比較,如果在兩個字符串中找到相似性數字,我如何獲得字符串作爲結果顯示?

串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像下面結果將在單元格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:\ TEMP \ 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