【问题标题】:Excel data validation reference spreadsheet location versus static listExcel 数据验证参考电子表格位置与静态列表
【发布时间】:2012-03-20 15:09:58
【问题描述】:

我在某些 Excel 数据验证方面遇到了问题。在一张纸上,我有一个值列表,其中包含填充验证下拉列表的列表,如下所示:

A1 |1.) 新项目 A2 |2.) 其他 A3 |3.) 变更单 A4 |4.) 选择要拆分的项目: A5 |填缝剂 NP-1 A6 |安全标志 A7 |警戒线 A8 |雪栅栏 A9 |排水管 4 磅 30"x30" A10 |不能脱 A11 |螺丝 1 5/8" A12 | SS 钣金 24 规格 A13 |钣金铝 040 磨光面 A14 |钣金铝 .050 铣削光洁度 A15 |终端栏 A16 |铅靴 2" A17 |铅靴 3" A18 |改性沥青顺滑APP A19 | TREMCO ELS 乳香 A20 | TREMCO Polyroof SF A21 | TREMCO 基片紧固件 A22 | TREMCO BURmastic 复合材料 A23 | TREMCO PowerPly 标准 A24 | TREMCO BURmastic SF A25 | TREMCO PowerPly HD 基片 A26 | TREMCO PowerPly Standard 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 | Sqeegies 24" 缺口 A45 |垃圾箱 A46 |约翰门 A47 |允许 A48 |分包商 RK Hydro Vac A49 |分包商屋顶 ICG A50 |分包商防雷 A51 |杂项 A52 |小计

当我有另一个工作表引用此列表时,验证工作正常。但是,我有一个 VBA 宏,它在某些时候将另一个工作表复制到工作簿中,以便通过电子邮件发送。由于这个和其他原因,我需要此列上的验证下拉列表不依赖于其他工作表,因此我让宏循环遍历所有单元格并创建一个如下所示的验证字符串:

1.) 新项目,2.) 杂项,3.) 变更单,4.) 选择被破坏的项目
:, Caulk NP-1 , 安全标志, 警告线

如您所见,它是完全相同的列表,但它是一个文本字符串,每个选择以逗号分隔。它几乎就像我需要的那样工作,但有一个问题 - 10 个前导空格没有像在第二种情况下那样工作。我将它们放在那里,以便这些选择在下拉框中缩进以使其更直观。前 10 个空格在实际的“验证列表公式”中,但是当我单击下拉列表或进行选择时不显示!

有什么想法吗?

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

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

因此,使用间接公式填充验证的代码的最后一部分是正常工作的部分。稍后在代码中,我使用字符串填充“静态”验证列表。这是另一部分:

    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

非常感谢您的帮助。这真的令人沮丧。

【问题讨论】:

    标签: validation excel vba


    【解决方案1】:

    使用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 个字符。

    替代

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

    【讨论】:

    • 好的,所以起初它有效,但经过进一步审查,它不是。虽然 Excel 仍处于打开状态,但它似乎工作正常。但是,如果我尝试保存文件然后重新打开 Excel,则表示文件中存在必须修复的错误。当它修复时,它说它修复了验证,实际上已经删除了它......
    • 我无法重现该问题。即使在我关闭它并再次打开它之后,该文件也可以正常工作。你能分享你正在使用的确切代码吗?
    • 好的,我将编辑原始问题以包含代码和填充下拉列表的完整项目列表
    • 代码没有问题 ;) 是数据。您的数据有引号(“”),因此当您连接字符串时,即使是引号也会连接起来,这会给您带来错误。让我看看我能不能做点什么:)
    • 呜呜呜。哇。用 2 个引号替换所有引号的 Substitute 函数怎么样?
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多