简体   繁体   中英

Excel VBA ComboBox error

I am trying to fill a comboxbox located in sheet "AAA" with data in Sheet "BBB"

x = Application.WorksheetFunction.Max(Sheets("BBB").Range("m2:m200")) + 1
Sheets("AAA").Shapes("Drop_Leg").Select
With Selection
    .ListFillRange = "='BBB'!n2:n" & x
End With

I am getting the error

"Requested Shapes are locked for selection".

I have tried different approaches, but cannot get it to work. This used to be a dropbox, but I was asked to change it to a combo.

Thanks in advance

Try the code below to populate your Active-X Combo-Box .

There's no need to use Select to populate the Combo-Box , it only slows down your code's run-time.

Code

Option Explicit

Sub FillCombo()

Dim x           As Long
Dim ComboRng    As Range

x = Application.WorksheetFunction.Max(Sheets("BBB").Range("M2:M200")) + 1
Set ComboRng = Sheets("BBB").Range("N2:N" & x) '<-- set the Range

With Sheets("AAA").OLEObjects("Drop_Leg").Object
    .Clear ' clear before adding new values
    .List = ComboRng.Value ' populate the list with values from Column N
End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM