添加activeX Combobox直到最后一个完整行

时间:2014-07-23 14:39:22

标签: excel-vba combobox activex excel-2007 vba

如何将activeX组合框添加到列“列表名称”中,直到网格的最后一行?知道在网格中唯一一个满是值的列是“C Type”

我试过了:

For Each Ws In ActiveWorkbook.Sheets
        With Ws
            'adding the validation dropdown
             Col_num = .Range("A1").End(xlToRight).Column
             Set MyRange = .Range(.Cells(1, 1), .Cells(1, Col_num)).Cells.Find("List Name")
             Set FullRange = .Range(.Cells(1, 1), .Cells(1, Col_num)).Cells.Find("C Type")

             'LastLine = .Range(FullRange).End(xlDown).Row
             LastLine = .Cells(2, FullRange).End(xlDown).Row

            Do Until IsEmpty(LastLine)
                If Not MyRange Is Nothing Then
                    With Ws.Columns(MyRange.Column).Validation
                     .Delete
                     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                      xlBetween, Formula1:=Liste
                      .IgnoreBlank = True
                      .InCellDropdown = True
                    End With
                End If
             Loop
        End With
    Next Ws

但它在此行中给出了“对象_worksheet失败的方法范围”错误:

LastLine = .Range(FullRange).End(xlDown).Row。 当我尝试以下操作时,它给出了错误“类型不匹配” LastLine = .Cells(2,FullRange).End(xlDown).Row

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用:

LastLine = .Cells(.Rows.Count, FullRange.Column).End(xlUp).Row

修订完整代码:

For Each Ws In ActiveWorkbook.Sheets
    With Ws
        'adding the validation dropdown
        Col_num = .Range("A1").End(xlToRight).Column
        Set myrange = .Rows(1).Find("List Name")
        If Not myrange Is Nothing Then
            Set fullrange = .Rows(1).Find("C Type")
            If Not fullrange Is Nothing Then
                'LastLine = .Range(FullRange).End(xlDown).Row
                lastline = .Cells(.Rows.Count, fullrange.Column).End(xlUp).Row

                With .Range(.Cells(2, myrange.Column), .Cells(lastline, myrange.Column)).Validation
                    .Delete
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                         xlBetween, Formula1:=Liste
                    .IgnoreBlank = True
                    .InCellDropdown = True
                End With
            End If
        End If
    End With
Next Ws