【发布时间】: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 的用途