我正在尝试将值的字符串复制到新工作表的单列中。我的代码在活动单元格中只有一个值时起作用,但是一旦有多个值,它将复制单元格中的每个值。我希望它仅将最新添加的内容复制到新工作表的列中。输入是来自允许多个选择的下拉菜单的选择。然后,我将这些选择拆分并偏移到9列以上的新单元格中(我也有其他下拉菜单,因此这就是为什么有这么多空间的原因,但是较大的循环应该能够处理其他下拉菜单)。
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & "; " & Newvalue
Dim txt As String
Dim i As Integer
Dim FullName As Variant
txt = ActiveCell.Value
FullName = Split(txt, ";")
For i = 1 To UBound(FullName)
ActiveCell.Offset(i, 9).Value = FullName(i)
ActiveCell.Offset(i, 9).Copy
Worksheets("Links").Range("A3").End(xlUp).Offset(2, 0).Insert
Next i
为了简化查找解决方案,我仅包含了有问题的代码循环。
答案 0 :(得分:0)
我最好的猜测是,在检测到更改时,是否要更新9个单元格中的与众不同的值列表?
现在,您已经在管理一个单独的列表。您所需要做的就是清除第9列中的值,然后在下拉菜单中打印这些值。
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$A$1" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else:
If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & "; " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
Dim txt As String
Dim i As Integer
Dim FullName As Variant
txt = ActiveCell.Value
FullName = Split(txt, ";")
ActiveCell.Offset(, 9).EntireColumn.Clear
For i = 0 To UBound(FullName)
ActiveCell.Offset(i, 9) = Trim(FullName(i))
Next i
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
但是,如果我想从一个或多个下拉列表中找到一个不同的列表怎么办?定界数组?管理其中的唯一列表的最佳方法是Collection或Dictionary对象。
如果这是您要寻找的内容,我将使用一种使用这些对象的方法来更新此答案。
根据您的反馈,我已将代码更新为以下代码,以使用集合对象从多个下拉列表中管理您的独特列表。
Option Explicit
Private col As Collection
' ^ we are defining this to the module level. That means it will retain values
' and be able to be referenced from any other place in the project.
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Not Intersect(Target, Range("$A$1:$B$1")) Is Nothing Then
' ^ this will make the area your looking more specific than just .row = 11
' you could also replace the address with a namedRange
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else:
If Target.Value = "" Then
'' My guess is that here you would want to make a call to a function that
'' removes values from the function. You should be able to loop over the collection
'' to find the value to remove.
GoTo Exitsub
Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & "; " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
ManageList Newvalue
' ^ you already have the newest value. You just need a easy way to check if it
' is in the list. To do this I made a sub that receives text, and checks
' if it is in the publicly scoped collection.
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
Private Sub ManageList(txt As String)
' This Sub will receive a text value and try to put it in a collection.
If col Is Nothing Then Set col = New Collection
On Error Resume Next
col.Add Item:=txt, Key:=txt
' ^ this method will throw an error if the Key is already in the collection.
' all we need to do then is watch for errors, and handle if we found a new one.
' I have found that collections and dictionary objects can handle .5M keys without any issues.
' using a dictionary, would allow you to test for a key without defining an error handler.
' with the trade off being that you have to add an additional reference to your project.
If Err.Number = 0 Then
' we had a new value
PrintList col
End If
End Sub
Private Sub PrintList(col As Collection)
Dim printTo As Range
Dim i As Long
Set printTo = Range("e1")
' ^ change e1 to a fully qualified address of where you
' want you list to be printed.
printTo.EntireColumn.Clear
On Error GoTo eos:
For i = 0 To col.Count - 1
printTo.Offset(i) = col(i + 1)
Next
eos:
End Sub