简体   繁体   English

excel vba:如何将组合框值粘贴到下一个列单元格

[英]excel vba: How to paste the combobox value to the next column cell

I am new to vba, I would like to ask how can I paste the item I am selecting from combobox in each cell? 我是vba的新手,我想问一下如何在每个单元格中粘贴从组合框中选择的项目?

Example: Combobox2 items are 10-STD, 12-40, 8-STD. 示例:Combobox2项是10-STD,12-40、8-STD。

I will select 10-STD and it should paste on column E9, next I will select 12-40 it should paste on the next empty cell E10, same thing with 8-STD when selected it should paste on E11. 我将选择10-STD并将其粘贴在E9列上,接下来,我将选择12-40并将其粘贴在下一个空单元格E10上,与8-STD相同的内容应将其粘贴在E11上。

Thanks for the kind help. 感谢您的帮助。

For the Form Control "Combo Box" the following code will work: 对于表单控件“组合框”,以下代码将起作用:

Sub DropDown1_Change()
    Dim dD As Object
    Dim selectedValue As String

    Set dD = DropDowns("Drop Down 1")

    selectedValue = dD.List(dD.ListIndex)

    If Range("E9") = Empty Then
        Range("E9") = selectedValue
    Else
        lrow = Cells(Rows.Count, 5).End(xlUp).Row
        Cells(lrow + 1, 5) = selectedValue
    End If  
End Sub

For the ActiveX ComboBox it is the following: 对于ActiveX ComboBox,它是以下内容:

Private Sub ComboBox1_Change()
    Dim cB As ComboBox
    Dim selectedValue As String

    Set cB = OLEObjects("ComboBox1").Object

    selectedValue = cB.Value

    If Range("E9") = Empty Then
        Range("E9") = selectedValue
    Else
        lrow = Cells(Rows.Count, 5).End(xlUp).Row
        Cells(lrow + 1, 5) = selectedValue
    End If
End Sub

EDIT: As QHarr said, the code can be enbeeded in the Worksheet Code, so it works without Dim ws As Worksheet Set ws As ActiveWorksheet Code. 编辑:正如QHarr所说,该代码可以包含在工作表代码中,因此它可以在不Dim ws As Worksheet Set ws As ActiveWorksheet代码的情况下工作。 Also Range("E9") = Empty can be replaced by isEmpty(Range("E9")) 也可以用isEmpty(Range("E9"))替换Range("E9") = Empty

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

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