Excel数据validation参考电子表格位置与静态列表

我遇到了一些Excel数据validation问题。 在一张表中,我有一个包含填充validation下拉列表的值列表,如下所示:

 A1 | 1。)新项目
 A2 | 2。)杂项
 A3 | 3。)更改订单
 A4 | 4。)select正在分解的项目:
 A5 | 克洛克NP-1
 A6 | 安全标志
 A7 | 警告线
 A8 | 雪篱笆
 A9 | 排水导线4磅30“x30” 
 A10 | 不可能的地带
 A11 | 螺丝1 5/8“ 
 A12 |  SS钣金24号
 A13 | 钣金铝040磨完成 
 A14 | 钣金铝0.050磨完成
 A15 | 终止酒吧
 A16 | 铅靴2“
 A17 | 铅靴3“ 
 A18 | 改性沥青平滑的APP
 A19 |  TREMCO ELS乳香
 A20 |  TREMCO Polyroof SF
 A21 |  TREMCO底板紧固件
 A22 |  TREMCO BURMATIC复合材料
 A23 |  TREMCO PowerPly标准
 A24 |  TREMCO BURmastic SF
 A25 |  TREMCO PowerPly HD底座
 A26 |  TREMCO PowerPly标准FR
 A27 |  TREMCO Burmesh 6“
 A28 |  TREMCO网站访问
 A29 |  TREMCO Reglet密封胶
 A30 |  TREMCO WB底漆
 A31 |  TREMCO冰涂料
 A32 |  TREMCO Tremflash磁带
 A33 |  TREMCO保修
 A34 |  1/4“x 1 1/2”驱动器引脚
 A35 |  SS屋顶钉子1 1/4“
 A36 | 货物
 A37 | 汽车燃料
 A38 |  PA直接劳工监察员
 A39 |  PA直接劳工钣金
 A40 |  PA直接顶棚屋面涂层
 A41 | 设备起重机45吨
 A42 | 设备起重机70吨
 A43 | 平台提升机R&G 400 28'
 A44 | scheme24“缺口
 A45 | 垃圾箱
 A46 |  Porta John
 A47 | 许可证
 A48 | 分包商RK Hydro Vac 
 A49 | 分包商屋顶ICG
 A50 | 分包商防雷
 A51 | 杂项
 A52 | 小计

当我有另一个工作表参考这个列表,validation工作正常。 然而,我有一个VBA的macros,在某些时候,将其他表单复制到一个工作簿本身通过电子邮件发送。 由于这个和其他原因,我需要这个列的validation下拉列表不依赖于另一个表,所以我有macros循环通过所有的单元格,并创build一个validationstring,看起来像这样:

   1.)新build项目,2。)杂项,3。)更改订单,4。)select要破碎的项目 
出::Caulk NP-1,安全标志,警告线

正如你所看到的,它是完全一样的列表,但是它是一个文本string,每个select用逗号分隔。 它的工作原理与我所需要的几乎一样,但是有一个问题 – 在第二种情况下,10个主要空间不能像他们应该那样工作。 我有他们在那里,这些select缩进下拉框,使其更直观。 10个领先的空间是在实际的“validation列表公式”中,但是当我点击下拉菜单或select时不显示!

有任何想法吗?

编辑:根据要求,这里是实际的代码:

Range("A1").Value = "1.) New Item" Range("A2").Value = "2.) Miscellaneous" Range("A3").Value = "3.) Change Order" Range("A4").Value = "4.) Choose the item being broken out:" Range("A5:A350").Formula = "="" ""&INDIRECT(""'Buy Out'!B""&MATCH(""Description"",'Buy Out'!$B:$B,0)+ROW()-3)" Application.Calculate ' build ValidationList string for later use (this will also have to happen when misc section isn't built, so this isn't the best place for it) Range("A1").Select ValidationList = ActiveCell.Value ActiveCell.Offset(1, 0).Select Do Until ActiveCell.Row = 350 If ActiveCell.Value = " Subtotal" Or ActiveCell.Offset(3, 0).Value = " Subtotals" Then 'end of the loop Exit Do End If If ActiveCell.Value <> "" Then ValidationList = ValidationList & "," & vbTab & ActiveCell.Value End If ActiveCell.Offset(1, 0).Select If ActiveCell.Row = 349 Then 'this shouldn't ever happen ValidationListMessedUp = True 'MsgBox ("There appears to be a problem creating the drop-down list for the ""Addon Category/Item to Break Out"".") End If Loop 'back to buy out and populate validation as dynamic formula Sheets("Buy Out").Select Cells.Find(What:="Addon Category/Item to Break Out", after:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate '* EH and message? "Is this job on the most recent MBO template? It does not appear to have the words "Release #" anywhere on the Buy Out tab With Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(TotalRow - ActiveCell.Row - 2, 0)).Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:= _ "=INDIRECT(""reference!$A$1:$A$""&IFERROR(MATCH("" Subtotal"",reference!$A:$A,0)-1,IFERROR(MATCH("" Grand Total"",reference!$A:$A,0)-1,MATCH("" Subtotals"",reference!$A:$A,0)-3)))" .IgnoreBlank = True .InCellDropdown = True .InputTitle = "" .ErrorTitle = "" .InputMessage = "" .ErrorMessage = "" .ShowInput = True .ShowError = True End With 

所以这个代码的最后一部分用间接公式来填充validation是正确工作的。 稍后在代码中,我使用string填充“静态”validation列表。 这是另一块:

  With Range(ActiveCell.Offset(MiscStartRow - ActiveCell.Row + 1, 0), ActiveCell.Offset(TotalRow - ActiveCell.Row + 5, 0)).Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=ValidationList .IgnoreBlank = False .InCellDropdown = True .InputTitle = "" .ErrorTitle = "" .InputMessage = "" .ErrorMessage = "" .ShowInput = True .ShowError = True End With 

非常感谢帮忙。 这真让人沮丧。

使用vbTab

例如

 With Selection.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:="1,2,3," & vbTab & " 4,5" .IgnoreBlank = True .InCellDropdown = True .InputTitle = "" .ErrorTitle = "" .InputMessage = "" .ErrorMessage = "" .ShowInput = True .ShowError = True End With 

快照

在这里输入图像说明

希望这是你想要的?

跟进

使用上述方法有一个缺点。 DVlist不能超过255个字符。

备选

将列表复制到工作表的极左列( 列XFD(excel 2007/2010)或列IV(Excel 2003) ),以便将其隐藏,然后在DV中使用它