0

簡単にするために、ある Excel ワークブックのuserID数値を別の Excel ワークブックと一致させる必要があります。アイデアは、2 つのドキュメントの大きい方が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

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 に答える