通过代码插入组合框

时间:2017-06-06 13:52:35

标签: excel excel-vba loops vba

我正在尝试在用户填充的单元格的右侧插入一个“PASS / FAIL”标准的下拉列表。将按顺序从上到下填充单元格。以下是我到目前为止的情况:

Sub ComboBoxPassFail()      
    Dim i As Integer
    Dim rng As Range

    i = 4

    For Each c In Worksheets("Inspection Report").Range("A4", Range("A4").End(xlDown)).Cells
        If c.Value <> "" Then

        Set rng = c.Offset(0, 14)

        Top = rng.Top
        Left = rng.Left

        ActiveSheet.OLEObjects.Add(classtype:="forms.combobox.1", Left:=Left, Top:=Top, Width:=8, Height:=15).Insert

        i = i + 1

        End If
    Next c

    i = 4

End Sub

我的主要问题来自处理组合框。

1 个答案:

答案 0 :(得分:0)

一些事情

  1. topleft是VBA中的保留字。您不能将它们用作变量
  2. 由于您使用的是Excel行,因此请将行声明为Long而不是Integer。顺便说一句,我没有在代码中看到使用i
  3. .Insert无效声明。
  4. 这是你在尝试的吗?

    Sub ComboBoxPassFail()
        Dim rng As Range, c As Range
        Dim rngTop As Double, rngLeft As Double
    
        For Each c In Worksheets("Inspection Report").Range("A4", Range("A4").End(xlDown)).Cells
            If c.Value <> "" Then
                Set rng = c.Offset(0, 14)
                rngTop = rng.Top
                rngLeft = rng.Left
    
                '~~> You may want to decide on a more appropriate width value? :)
                ActiveSheet.OLEObjects.Add classtype:="forms.combobox.1", _
                                           Left:=rngLeft, Top:=rngTop, _
                                           Width:=8, Height:=15
            End If
        Next c
    End Sub