所以我希望编写一个基本上可以选择范围的宏,将单元格中的值与数组中的值进行比较,并将缺少的值添加到范围的末尾。
答案 0 :(得分:1)
使用字符串尝试此示例:
为了让你开始,你可以使用Cells(行#,列#)而不是Range(" A1")并在数组上执行循环并逐行循环遍历,比较每个值。然后将不匹配的值添加到新数组中,稍后您将在另一个循环中传回表单。
假设你的价值从表格开始(" Sheet1")。范围(" A1")。使用以下示例名称: 山姆, 吉姆 斯坦利, 杰夫, 麦克风, 杰夫, 托比。
Dim lastRow As Integer
Dim i As Integer
Dim r As Integer
Dim n As Integer
Dim unMatchedArray() As String
Private Sub ArrayChecker()
Dim myArray(7) As String
Dim match As Boolean
n = 0
myArray(0) = "Jeff"
myArray(1) = "Stanly"
myArray(2) = "Mike"
myArray(3) = "Sam"
myArray(4) = "Toby"
myArray(5) = "Reginald"
myArray(6) = "Wolfgang"
myArray(7) = "Manual"
Call GetLastRow
For i = 0 To UBound(myArray)
r = 1
match = False
For r = 1 To lastRow
If myArray(i) = Sheets("Sheet1").Cells(r, 1) Then
match = True
Exit For
End If
Next r
If match = False Then
ReDim Preserve unMatchedArray(n)
unMatchedArray(n) = myArray(i)
n = n + 1
End If
Next i
n = n - 1
If n > 0 Then
Call AddToSheet
End If
End Sub
Private Sub GetLastRow()
' checking Range("A1:A65536")
lastRow = Sheets("Sheet1").Cells(65536, 1).End(xlUp).Row
End Sub
Private Sub AddToSheet()
Call GetLastRow
r = lastRow + 1
i = 0
For i = 0 To n
Sheets("Sheet1").Cells(r, 1) = unMatchedArray(i)
r = r + 1
Next i
End Sub