0

为简单起见:我需要将userID一个 Excel 工作簿中的数字匹配到另一个。这个想法是查看两个文档中较大的一个是否具有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
4

2 回答 2

1

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

于 2013-04-05T21:10:57.287 回答
0

可能有一种更有效的方法来执行此操作,使用嵌套循环,但这基本上是我认为您需要做的:

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
于 2013-04-05T22:08:00.577 回答