在不同生成的工作表上填充activeX组合框(Excel 2007)

时间:2014-07-14 15:15:33

标签: excel vba excel-vba combobox

我是一名初学者,因为我正在寻找一个徒劳的解决方案,这是我的问题:

在我的excel文件中,我应该使用宏生成不同的工作表(已经完成)。 现在,在所有这些生成的纸张上的指定列“L”上(从第9页开始),我需要一个activeX组合框,其中包含来自工作表(6)的无限列(A)的值。

我首先通过添加值<>来获取值。 “”并将它们存储在一个数组中。 它不起作用,一切都很乱,有人可以请更正我的代码并帮助我,我会感激任何帮助..提前谢谢

Option Explicit

Sub PicklistCopy()
    Dim Nbre As Byte, Arr(), Liste As String, Cptr As Byte
    Dim Current As Worksheet

    Dim strSearch As String
    Dim aCell As Range
    strSearch = "Pick List Name"

 For Each Current In Worksheets

    Set aCell = Rows(1).Find(What:=strSearch, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

    'copy pl values in an array
    With Sheets("Pick Lists")
         Nbre = Application.CountA(.Range("MyPL"))
         ReDim Arr(1 To .Range("MyPL").Count)
         Arr = Application.Transpose(.Range("MyPL"))
    End With
    'Get values diff from null
    For Cptr = 1 To UBound(Arr)
         If Arr(Cptr) <> "" Then Liste = Liste & Arr(Cptr) & ";"
    Next
    Liste = Left(Liste, Len(Liste) - 1)


    With ActiveSheet.Range("L2:L4").Validation
         .Delete
         .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
         xlBetween, Formula1:=Liste
        .IgnoreBlank = True
         .InCellDropdown = True
    End With
    ActiveSheet.Range("L2:L4") = ""

 Next
End Sub

1 个答案:

答案 0 :(得分:0)

我认为您要添加的是数据验证下拉列表,而不是常规下拉列表。下面是我在验证单元格下的列表的简单代码。

Sub AddMyPl()
 Dim ws As Worksheet
 Dim strSearch As String
Dim aCell As Range
strSearch = "Pick List Name"
 'This will loop thru my sheets in my workbook.
    For Each ws In ActiveWorkbook.Worksheets

  'skip the sheet that contains mylist
    If ws.Name <> "mylist" Then
    'get last row # in L column.
LastR = ws.Cells(Rows.Count, "L").End(xlUp).Row
'add validation from MyPL list to cells
    With ws.Range("L2:L" & LastR).Validation
     .Delete
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
     xlBetween, Formula1:="=MyPL"
    .IgnoreBlank = True
     .InCellDropdown = True
End With
    End If

    Next ws

End Sub

我有4张。 &#39; MYLIST&#39;工作表包含MyPL范围以及我的下拉列表项。循环将检查所有名称不是&#34; mylist&#34;然后将验证下拉列表添加到范围L2:L4。 根据需要修改。

相关问题