宏VBA - 比较两个字符串中的相似数字

时间:2016-04-04 02:06:49

标签: excel vba excel-vba

我是Macro VBA的新手,我遇到了一个问题。

我有两个要比较的字符串,如果在两个字符串中找到相似性数字,如何显示结果显示的字符串?

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

string 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

谢谢。

4 个答案:

答案 0 :(得分:1)

使powershell自动将列表打印到文本文件 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

答案 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

答案 2 :(得分: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

答案 3 :(得分:0)

试试这个

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">

  <div>external div</div>
  <div>controllerData: {{data}}</div>
  <div>dataFormDirective: {{dataFormDirective}}</div>
  </br>

  <div ng-controller="myCtrl">
    myCtrl
    <div>controllerData : {{data}}</div>
    <div>dataFormDirective:{{dataFormDirective}}</div>
  </div>
  </br>

  <div my-directive>myDirective {{dataFormDirective}}</div>

</body>

可以按如下方式调用

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