我想插入一个数据验证列表,用于从另一个工作表中获取数据。列表将生成的列存储在另一个动态变量中。到目前为止我的代码是:
pRange = Sheets("Payer Output").Cells(24, 3).Value
With Sheets("Payer Output").Range("C23").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=Sheets("Payers in Top 4").Cells(3, pRange)
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
此公式生成一个列表,并查找正确的列,但是当前代码仅指定列表应在单个单元格中查找,因此在生成列表时,只有一个值。如何为'formula1:='行指定一系列单元格?我试过了:
Formula1:=Sheets("Payers in Top 4").Range(Cells(3, pRange), Cells(10,pRange))
但这不起作用。谢谢你的帮助!
答案 0 :(得分:2)
有时我只会命名范围并将命名范围放在列表中。
Sub AddDtaVal()
pRange = Sheets("Payer Output").Range("C24").Value
Sheets("Payers in Top 4").Range(Sheets("Payers in Top 4").Cells(3, pRange), Sheets("Payers in Top 4").Cells(10, pRange)).Name = "List"
With Sheets("Payer Output").Range("C23").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=List"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
答案 1 :(得分:1)
您需要使用Excel格式的字符串构建地址公式:'Sheet Name'!A1:A10
Dim StrFormula As String
Dim pRange As Long
pRange = Sheets("Payer Output").Cells(24, 3).Value
With Sheets("Payers in Top 4")
StrFormula = "'" & .Name & "'!" & .Range(.Cells(3, pRange), .Cells(10, pRange)).Address
End With
With Sheets("Payer Output").Range("C23").Validation
.Delete
.Add Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, _
Formula1:=StrFormula
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
答案 2 :(得分:-1)
试试这个
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="='Payers in Top 4'!" & Worksheets("Payers in Top 4").Cells(3, pRange).Resize(8).Address