Excel 覆盖数据并仍保留数据下拉列表数据
我想知道是否可以用新数据覆盖行,但仍将以前的数据保留在下拉列表中? 我试图做的是循环遍历一个关联列表,如果条件为真,则在新行中添加数据,获取该数据并创建一个下拉列表,然后移动到下一个关联。我的代码可以工作,但它添加到了以前的数据中,只是让下拉列表变大了。在内部循环完成后,我还在第(3)列上尝试了clearContents,但它清除了先前的drop下行列表数据Excel 覆盖数据并仍保留数据下拉列表数据,excel,vba,copy-paste,Excel,Vba,Copy Paste,我想知道是否可以用新数据覆盖行,但仍将以前的数据保留在下拉列表中? 我试图做的是循环遍历一个关联列表,如果条件为真,则在新行中添加数据,获取该数据并创建一个下拉列表,然后移动到下一个关联。我的代码可以工作,但它添加到了以前的数据中,只是让下拉列表变大了。在内部循环完成后,我还在第(3)列上尝试了clearContents,但它清除了先前的drop下行列表数据 For j = 2 To GetRowLength("HR") If shHR.Range("B"
For j = 2 To GetRowLength("HR")
If shHR.Range("B" & j) = shIS.Range("F" & i) Then
shHR.Range("C" & GetRowLength("HR", 3) + 1) = shHR.Range("A" & j)
End If
Next j
With shIS.Range("O" & i).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=HR!$C:$C"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next i
For i = 2 To GetRowLength("Interview_Schedule")
For j = 2 To GetRowLength("HR")
If shHR.Range("B" & j) = shIS.Range("F" & i) Then
shHR.Range("C" & GetRowLength("HR", 3) + 1) = shHR.Range("A" & j)
End If
Next j
Dim arr()
arr = Application.Transpose(shHR.Range("C:C").SpecialCells(xlCellTypeConstants).Value)
With shIS.Range("O" & i).Validation
.Delete
ReDim Preserve arr(LBound(arr) To UBound(arr) + 1)
arr(UBound(arr)) = shIS.Range("O" & i)
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(arr(), ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next i
您可以在Formula1定义中使用动态数组。 注意下面的代码,但我不确定您是否正在查找:
注意:此解决方案有一个限制->LEN(Join(arr(),“,”))Excel验证列表,从给定范围获取数据将始终显示从该范围到下拉列表的更新值。嗨,Barney,这实际上是我的第一个方法,但下一行只是附加到以前的关联。我需要新数据,我知道了。每次迭代后,我都会擦除工作表。我将在第一个示例上方发布更新后的代码。@SJD如果我正确理解了您的注释,您是否已将问题中的代码更改为包含解决方案?所以这个问题不能再与问题中的代码重复,它会使这个答案无效吗?这不是堆栈溢出的设计用途。如果barneyo的答案解决了问题,则应单击其左侧的复选标记将其标记为“答案”。如果您的解决方案与该建议存在显著差异,则应将您的代码及其解释作为新答案发布。我正在回滚您最近的编辑,以便该问题对未来用户仍然有效。
Dim bAddNewItem As Boolean
Dim arr()
' Fill out an array once somewhere at the beginning of the procedure
arr = Application.Transpose(Sheet("HR").Range("C:C").SpecialCells(xlCellTypeConstants).Value)
' If You want to add new item change to True
bAddNewItem = False
With Selection.Validation
.Delete
If bAddNewItem Then
ReDim Preserve arr(LBound(arr) To UBound(arr) + 1)
arr(UBound(arr)) = Selection.Value
End If
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(arr(), ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
' Add this line before updating Column C
ActiveWorkbook.Names.Add name:="myRng", RefersTo:="=" & Sheet("HR").Range("C:C").SpecialCells(xlCellTypeConstants).Address
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=myRng"
' rest of code