我坚持添加ActiveX组件。我在Excel 2007中使用VBA添加ActiveX ComboBox时收到错误代码424。 我希望这里有人会这么善良并帮助我。
Sub addComboBx()
Dim countColl As Double
Dim cbx_name As String
Dim objChBx As OLEObject
Dim comboBx As ComboBox
Dim rng As Range
Dim rowNr As Double
rowNr = 27
lastRow = 115
LastRange = "B2: F" & lastRow
arrayCountry = Worksheets("Sheet 1").Range(LastRange).Value
countColl = countRows(rowNr) 'Sub that counts rows from line X untill the value of checked cell in next row is equal "" (empty string)
Worksheets("Sheet 2").Cells((rowNr + countColl), 1).EntireRow.Insert
cbx_name = "Cbx_countries_list_" & (countColl + 1)
Set rng = Worksheets("Sheet 2").Cells((rowNr + countColl), 3)
'-> In line below I have Error 424 'Object required'
With Worksheets("Sheet 2").OLEObjects.Add(ClassType:="Forms.ComboBox.1", link:=False, DisplayAsIcon:=False, Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
With .Object
For a = LBound(arrayCountry) To UBound(arrayCountry)
.AddItem arrayCountry(a, 1)
Next a
End With
Set comboBx = .Object
With comboBx
.Value = Sheets("Sheet 2").Cbx_countries_list.Value
.Placement = xlMove
.PrintObject = True
.Name = cbx_name
End With
End With
End Sub
我无法理解,为什么我有这个424错误。我检查了这个和其他论坛,并没有找到解决方案。
以下是发生错误的行:
With Worksheets("Sheet 2").OLEObjects.Add(ClassType:="Forms.ComboBox.1", link:=False, DisplayAsIcon:=False, Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
我不知道它是否重要。上面的代码位于单独的Module中,而不是在Worksheet中,因为它在单击按钮后被调出