我正在尝试编写一个VBA宏来格式化同一工作簿中2张纸中的一部分的数据,然后一旦格式化完成,宏应将每张纸中的4列彼此进行比较,如果每个工作表中的4列组合之间存在匹配项,然后应在Oracle工作表范围的末尾的空白列中填充2个字符串中的1个。比较之后,如果该列中的任何单元格为空白,则应说找不到人。
尽管有问题,但它是在所有未找到人员的单元格中填充的,如果数据正确且需要进行匹配,我已经通过计数进行了比较。
到目前为止,我的代码如下:
Sub CompareValues()
Dim bank As Worksheet
Dim Oracle As Worksheet
Dim cell As Range
Dim i As Long, J As Long
Dim OracleArr As Variant, bankArr As Variant
Dim match As Boolean
Dim Oraclerng As Range
Dim bankRng As Range
Set Oracle = ActiveWorkbook.Sheets("Oracle Fusion File")
Set bank = ActiveWorkbook.Sheets("Bank File")
Set Oraclerng = Oracle.Range("A2", Oracle.Cells(Rows.Count, "A").End(xlUp).Offset(0, 10))
Set bankRng = bank.Range("A2", bank.Cells(Rows.Count, "A").End(xlUp).Offset(0, 11))
OracleArr = Oraclerng.Value2
bankArr = bankRng.Value2
For i = 1 To UBound(OracleArr)
match = False
For J = 1 To UBound(bankArr)
If OracleArr(i, 1) = bankArr(J, 8) And OracleArr(i, 6) = bankArr(J, 4) And OracleArr(i, 7) = bankArr(J, 5) And OracleArr(i, 8) = bankArr(J, 8) Then
If match Then
OracleArr(i, 10) = "Complete match"
Else
OracleArr(i, 10) = "Investigation Required"
End If
End If
Next J
Next i
For i = 1 To UBound(OracleArr)
If OracleArr(i, 10) = "" Then
OracleArr(i, 10) = "Person not found"
End If
Next i
Oraclerng.Value = OracleArr
End Sub
我非常感谢任何帮助,因为我真的是VBA的新手,但并不是那么擅长,所以我正在努力找出我要去哪里哪里
非常感谢,
马特
答案 0 :(得分:0)
很难确切地说出这里出什么问题了,但是根据我上面的评论,我认为以下内容将使您更加接近。您的if match Then...
测试是多余的,因为从未设置match
。相反::
Sub CompareValues()
Dim bank As Worksheet
Dim Oracle As Worksheet
Dim cell As Range
Dim i As Long, J As Long
Dim OracleArr As Variant, bankArr As Variant
Dim match As Boolean
Dim Oraclerng As Range
Dim bankRng As Range
Set Oracle = ActiveWorkbook.Sheets("Oracle Fusion File")
Set bank = ActiveWorkbook.Sheets("Bank File")
Set Oraclerng = Oracle.Range("A2", Oracle.Cells(Rows.Count, "A").End(xlUp).Offset(0, 10))
Set bankRng = bank.Range("A2", bank.Cells(Rows.Count, "A").End(xlUp).Offset(0, 11))
OracleArr = Oraclerng.Value2
bankArr = bankRng.Value2
'loop through Oracle Range
For i = 1 To UBound(OracleArr)
'set match to false for tihs iteration of i
match = False
'loop through bank range
For J = 1 To UBound(bankArr)
'Look for a match, if it's found, set match=true and exit the J loop since we found a hit already and any further searching would be a waste.
If OracleArr(i, 1) = bankArr(J, 8) And OracleArr(i, 6) = bankArr(J, 4) And OracleArr(i, 7) = bankArr(J, 5) And OracleArr(i, 8) = bankArr(J, 8) Then
match = true
exit for
End If
Next J
'while iterating through the bank range (j) did we find a match?
If match Then
OracleArr(i, 10) = "Complete match"
Else
OracleArr(i, 10) = "Investigation Required"
End If
Next i
'This is probably never going to hit because either match = true or match = false when iterating through `i` (oracle range) the first time. We would have either written `Complete Match` OR `Investigation Required` for every row already. You'll have to refine your logic here or above to get whatever this is supposed to be.
For i = 1 To UBound(OracleArr)
If OracleArr(i, 10) = "" Then
OracleArr(i, 10) = "Person not found"
End If
Next i
Oraclerng.Value = OracleArr
End Sub
我们遍历j
并尝试找到匹配项。如果找到一个,则将match
设置为true
,然后结束j
循环。然后我们看看是否有一个match=true
,如果有,我们适当地填充oraclearr(i,10)
。
我仍然认为您的IF
的{{1}}条件可能存在问题,其中OracleArr(i, 1) = bankArr(J, 8) And OracleArr(i, 6) = bankArr(J, 4) And OracleArr(i, 7) = bankArr(J, 5) And OracleArr(i, 8) = bankArr(J, 8)
进行了两次测试,但这也许就是这样。