简体   繁体   English

将Combobox的单击按钮移动到下一行Excel VBA

[英]Move Combobox onclick of a button to a next row excel VBA

I have created a vba code to create combobox and populated with my named range. 我已经创建了一个vba代码来创建组合框,并填充了我的命名范围。 Now i want to make when i select something from a combobox it has to set that value in that current cell and it should move to next row so that i can keep setting a value in each cell from a combobox. 现在,当我从组合框中选择某项内容时,我想使其在当前单元格中设置该值,并且它应该移至下一行,以便我可以继续在组合框中为每个单元格设置一个值。

i have this following code to create combobox , but i dont know how to make it move to a next row with .onaction 我有以下代码来创建组合框,但我不知道如何使用.onaction使它移动到下一行

Sub AddComboBoxes()
Dim cb As Object
Dim aCell As Range, r As Long


For i = 1 To 1
    Set aCell = Sheet1.Cells(i, 5)
    Set cb = Sheet1.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Left:=aCell.Left, Top:=aCell.Top, Width:=aCell.Width, Height:=aCell.Height)
    cb.Placement = xlMoveAndSize
    cb.Name = "ComboBoxN1"
    cb.ListFillRange = "N1"
    cb.OnAction = "N1.value"

Next

End Sub

Please help. 请帮忙。

Like I mentioned .OnAction is not a property of OLEObjects but of DropDowns 就像我提到的.OnAction不是OLEObjects的属性而是DropDowns的属性

See this example which will create the dropdowns 5 times in Col 5 and assign "Sheet2!A1:A5" as ListFillRange and execute the CallMe when you select an option in the DropDown. 参见此示例,该示例将在Col 5中创建下拉菜单5次,并将"Sheet2!A1:A5"分配为ListFillRange并在DropDown中选择一个选项时执行CallMe

LOGIC: 逻辑:

  1. Name your DropDowns like "ComboBoxN" & i in a loop so that we can retrieve the row they are on later. 在循环中将您的DropDown命名为"ComboBoxN" & i ,以便我们稍后可以检索它们所在的行。
  2. Your post says that the value of the combo should be set in the current cell. 您的帖子说,该组合的值应在当前单元格中设置。 I am using Col A cells in my example below. 我在下面的示例中使用Col A单元格。 Change as applicable. 进行相应的更改。
  3. I am assuming that your DropDowns start from row one. 我假设您的DropDowns从第一行开始。 If not then you will have to change the logic of ascertaining the row number in Sub CallMe() 如果不是,那么您将不得不更改在Sub CallMe()中确定行号的逻辑

Code: 码:

Sub AddComboBoxes()
    Dim cb As Object
    Dim aCell As Range
    Dim i As Long

    For i = 1 To 5
        Set aCell = Sheet1.Cells(i, 5)
        Set cb = Sheet1.DropDowns.Add(aCell.Left, aCell.Top, aCell.Width, aCell.Height)
        cb.Placement = xlMoveAndSize
        cb.Name = "ComboBoxN" & i
        cb.ListFillRange = "Sheet2!A1:A5"
        cb.OnAction = "CallMe"
    Next
End Sub

Your CallMe should be something like this 您的CallMe应该是这样的

Sub CallMe()
    Dim rw As Long
    Dim cb As Object

    '~~> Extract the number from the dropdown to
    '~~> identify which row is it in
    rw = Val(Trim(Replace(Application.Caller, "ComboBoxN", "")))

    Set cb = Sheet1.DropDowns(rw)

    '~~> We are setting the value in Col A. Chnage as applicable
    Sheet1.Range("A" & rw).Value = cb.Value

    '~~> Activate the next cell.
    Sheet1.Range("A" & rw + 1).Activate
End Sub

Screenshot: 屏幕截图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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