为了简单起见:我需要将一个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
答案 0 :(得分:1)
转到模块顶部,然后输入“Option Explicit”。你拼错了“MsgBox(seachString)”里面的searchString,因此创建了一个新的变量。
答案 1 :(得分: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