【问题标题】:How to compare two strings in different workbooks (Excel VBA)如何比较不同工作簿中的两个字符串(Excel VBA)
【发布时间】:2013-04-05 17:14:44
【问题描述】:

为简单起见:我需要将一个 Excel 工作簿中的 userID 数字匹配到另一个。这个想法是查看两个文档中较大的一个是否有来自较小文档的userID 数字。如果较大的文档有userID 编号,则需要将其他数据复制到较小的文档中(我知道该怎么做)

我的问题是,当我比较每个字段时,我的函数一直将我的 searchString(较大文档中的字符串)显示为空白。它不会像我在较小文档中创建的那样拾取我创建的数组。代码会比我解释得更好。我不是程序员,也不是真正了解 VBA,所以我已经知道我的代码低于标准。

每当我测试我的代码时,我都会让 MsgBox 函数向我显示它正在比较的字符串,由于某种原因,“searchString”总是显示为空白,因此它正在比较我的“findString”,其中包含它需要的数据,无论出于何种原因到一个空白字符串。我需要 MsgBox 函数来显示另一个文档中的数组数据,而不仅仅是一个空白框。

'NID Comparisson Script

Sub getUID()

'Select Appropriate cell
Range("C2").Select

'Count the number of rows that contain data in the SCCM document.
Dim rows As Integer
rows = ActiveSheet.UsedRange.rows.count

'Create Array
With Sheets("SMSReportResults(2)")
    arrData = .Range(.Cells(2, 3), .Cells(rows, 3)).Value
End With

'Declare a variable for comparing UID/NID strings
Dim findString As String

'Loop through the document and grab the UID numbers as "searchString"
For i = 1 To rows - 1
    findString = arrData(i, 1)
    Windows("NIDs.xlsx").Activate
    Dim rows2 As Integer
    rows2 = ActiveSheet.UsedRange.rows.count

    'Create second array in the NIDs Workbook
    With Sheets("Sheet1")
        arrData2 = .Range(.Cells(2, 1), .Cells(rows, 1)).Value
    End With

    'Create searchString for NIDs workbook
    Dim searchString As String

    For j = 1 To rows2 - 1
        searchString = arrData2(j, 1)

        Dim compare As Integer
        compare = StrComp(searchString, findString)
        MsgBox (seachString)
        MsgBox (findString)
        MsgBox (compare)
        ActiveCell.Offset(1, 0).Select

    Next
Next

End Sub

【问题讨论】:

  • 您可以为此编写代码,但这确实是 VLOOKUPS 的用途

标签: string excel vba compare


【解决方案1】:

转到模块顶部并键入“Option Explicit”。您在“MsgBox (seachString)”中拼错了 searchString,因此创建了一个新变量。

【讨论】:

    【解决方案2】:

    可能有一种更有效的方法来做到这一点,使用嵌套循环,但这基本上是我认为你需要做的:

    Dim rows        As Integer
    Dim arrData     As Variant
    Dim arrData2    As Variant
    
    rows = ThisWorkbook.ActiveSheet.UsedRange.rows.Count
    
    With Sheets("SMSReportResults(2)")
        arrData = .Range(.Cells(2, 3), .Cells(rows, 3)).Value
    End With
    
    On Error Resume Next
        Windows("NIDs.xlsx").Activate
    On Error GoTo 0
    
    With Workbooks("NIDS.xlsx").Sheets("Sheet1")
        arrData2 = .Range(.Cells(2, 1), .Cells(rows, 1)).Value
    End With
    
    For R1 = 1 To UBound(arrData, 1)
        For C1 = 1 To UBound(arrData, 2)
            For R2 = 1 To UBound(arrData2, 1)
                For C2 = 1 To UBound(arrData2, 2)
                    If arrData(R1, C1) = arrData2(R2, C2) Then
    
                        'Returns the value from the next column in NIDS
                        MsgBox Workbooks("NIDS.xlsx").Sheets("Sheet1").Cells(R2, C2 + 1)
    
                    End If
                Next C2
            Next R2
        Next C1
    Next R1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 2020-06-28
      相关资源
      最近更新 更多