通过代码插入combobox

我试图插入一个下拉列表,有一个“通过/失败”标准的用户填充单元格的权利。 细胞将从上到下依次填充。 这是我到目前为止:

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 

我的主要问题来自处理combobox。

几件事

  1. topleft是VBA中的保留字。 你不能使用它们作为variables
  2. 由于您正在使用Excel行,所以将行声明为Long而不是Integer 。 顺便说一句,我没有看到在代码中使用i
  3. .Insert是无效的声明。

这是你正在尝试?

 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