我试图从 A 列中提取字符串并将它们移动到 B 列,前提是它们在 B 列中尚不存在。为此,我想制作一个列表并用它扫描 A 列的所有内容,但是,我我不确定如何在 VBA 中做到这一点。在python中,我记得使用类似的东西
[If (x) not in (List)]
但同样的方法在 Excel 中对我不起作用。
目前,我有以下
Sub GatherAll()
GL = List()
rwcnt = WorksheetFunction.CountA(Range("A:A"))
lastc = Cells(1, Columns.Count).End(xlToLeft).Column
Dim i As Long
For i = 2 To rwcnt
Cells(i, 1).Value = n
我想说一些类似的话
if n not in GL, GL.append(n)
continue
End Sub
如果有人可以帮助我,我将不胜感激。
最佳答案
尝试根据您的确切需求调整以下代码,看看是否有帮助。如果您需要帮助,请告诉我们。
Sub MoveUniqueEntries()
Dim oDict As Object
Dim rToMove As Range
Dim rDest As Range
Dim rLoop As Range
Set oDict = CreateObject("Scripting.Dictionary")
Set rToMove = Intersect(Sheet1.Range("A1").CurrentRegion, Sheet1.Columns(1))
Set rDest = Sheet1.Range("B1")
For Each rLoop In rToMove
If oDict.exists(rLoop.Value) Then
'Do nothing
Else
oDict.Add rLoop.Value, 0
rDest.Value = rLoop.Value
Set rDest = rDest.Offset(1)
End If
Next rLoop
End Sub
关于arrays - Excel VBA 创建列表并仅添加唯一术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31684992/