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

时间:2012-03-20 15:09:58

标签: excel validation excel-vba vba

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

A1 |1.)   New Item
A2 |2.)  Miscellaneous
A3 |3.)  Change Order
A4 |4.)  Choose the item being broken out:
A5 |          Caulk NP-1
A6 |          Safety signs
A7 |          Warning lines
A8 |          Snow fence
A9 |          Drain Lead 4 lb 30"x30" 
A10 |          Cant Strip
A11 |          Screws 1 5/8" 
A12 |          SS Sheet Metal 24 gauge
A13 |          Sheet Metal Aluminum 040 mill finish 
A14 |          Sheet Metal Aluminum .050 mill finish
A15 |          Termination Bar
A16 |          Lead Boots 2"
A17 |          Lead Boots 3" 
A18 |          Modified Bitumen Smooth APP
A19 |          TREMCO ELS Mastic
A20 |          TREMCO Polyroof SF
A21 |          TREMCO Base sheet fasteners
A22 |          TREMCO BURmastic Composite
A23 |          TREMCO PowerPly Standard
A24 |          TREMCO BURmastic SF
A25 |          TREMCO PowerPly HD Base Sheet
A26 |          TREMCO PowerPly Standard FR
A27 |          TREMCO Burmesh 6"
A28 |          TREMCO Site visits
A29 |          TREMCO Reglet Sealant
A30 |          TREMCO WB Primer
A31 |          TREMCO Ice Coating
A32 |          TREMCO Tremflash tape
A33 |          TREMCO Warranty
A34 |          1/4" x 1 1/2" drive pins
A35 |          SS Roof Nails 1 1/4"
A36 |          Freight
A37 |          Auto Fuel
A38 |          PA Direct Labor Supervisor
A39 |          PA Direct Labor Sheet Metal
A40 |          PA Direct Labor Roof Coating
A41 |          Equipment Crane 45 ton
A42 |          Equipment Crane 70 ton
A43 |          Platform Hoist R&G 400 28'
A44 |          Sqeegies 24" Notched
A45 |          Dumpsters
A46 |          Porta John
A47 |          Permit
A48 |          Subcontractor RK Hydro Vac 
A49 |          Subcontractor Roofing ICG
A50 |          Subcontractor Lightning Protection
A51 |          Misc
A52 |          Subtotal

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

  1.)   New Item,2.)  Miscellaneous,3.)  Change Order,4.)  Choose the item being broken
out:, Caulk NP-1 , Safety signs, Warning lines

正如您所看到的,它是完全相同的列表,但它是一个文本字符串,每个选择用逗号分隔。它的工作方式几乎就像我需要的那样,但是存在一个问题 - 在第二种情况下,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

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

1 个答案:

答案 0 :(得分:2)

使用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

<强>快照

enter image description here

希望这是你想要的?

关注

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

<强> ALTERNATIVE

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