如何复制组合框以便单元格链接自动更改

发布于 2024-10-17 19:13:36 字数 563 浏览 1 评论 0原文

是否可以复制创建的组合框并将其粘贴到另一个单元格中,以便 该单元格链接也更改了吗?

我使用 Excel 2007 进行这项工作。

示例:我在 A5 中有组合框,单元格链接指向 B5。我想复制 组合框指向单元格 A4,单元格链接指向 B4。

我需要复制 50 多个组合框。当我按下包含宏的命令按钮来复制整行并将其全部插入到新行时,我尝试使用它。

我找到了一个答案,但 VBA 显示“编译错误:用户定义的类型未定义!”并且“TypeOf cbo.Object Is msforms.ComboBox”为粗体。

我发现的宏:

Sub Test()
    Dim cbo As OLEObject

    For Each cbo In ActiveSheet.OLEObjects
        If TypeOf cbo.Object Is msforms.ComboBox Then
            cbo.LinkedCell = cbo.TopLeftCell.Offset(, 1).Address
        End If
    Next
End Sub

Is it possible to copy a created combobox and paste it in another cell so
that cell link is changed too?

I do this work with Excel 2007.

Example: I have combobox in A5, cell link pointing to B5. I want to copy
the combo box to cell A4 and the cell link to point to B4.

I need to copy more than 50 comboboxes. I tried to use it when I push the commandbutton which include the macro to copy a entire row and insert all of it to the new row.

I found one answer but VBA showed "Compile error: User defined type not defined!" and "TypeOf cbo.Object Is msforms.ComboBox" is bold.

Macro I found:

Sub Test()
    Dim cbo As OLEObject

    For Each cbo In ActiveSheet.OLEObjects
        If TypeOf cbo.Object Is msforms.ComboBox Then
            cbo.LinkedCell = cbo.TopLeftCell.Offset(, 1).Address
        End If
    Next
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懵少女 2024-10-24 19:13:36

最初的答案让我走上了正轨,适用于“ActiveX 控件”。我需要使用标准 Excel“表单控件”组合框执行相同的操作,它非常相似,只是它是一个形状对象并且需要选择它才能进行操作。这是我的代码,对我有用:

Sub AllocateLinkedCellsToComobBoxes()

Dim myShape As Shape

For Each myShape In ActiveSheet.Shapes
    If myShape.Type = msoFormControl Then
        If myShape.FormControlType = xlDropDown Then
            myShape.Select
            Selection.LinkedCell = Selection.TopLeftCell.Address
        End If
    End If
Next

End Sub

The original answer put me on the right track and works for the "ActiveX control". I needed to do the same thing with a standard Excel "Form control" combobox and it's pretty similar, except it's a shape object and it needs to be selected to be manipulated. Here's my code, which worked for me:

Sub AllocateLinkedCellsToComobBoxes()

Dim myShape As Shape

For Each myShape In ActiveSheet.Shapes
    If myShape.Type = msoFormControl Then
        If myShape.FormControlType = xlDropDown Then
            myShape.Select
            Selection.LinkedCell = Selection.TopLeftCell.Address
        End If
    End If
Next

End Sub
少女的英雄梦 2024-10-24 19:13:36

Reima,

如果您使用“控件工具箱”工具栏中的控件,则以下内容应该有效。唯一的区别是它不是 MSForms.Combobox。 :

Sub Test()
Dim cbo As OLEObject

For Each cbo In ActiveSheet.OLEObjects
    If TypeOf cbo.Object Is ComboBox Then
        cbo.LinkedCell = cbo.TopLeftCell.Offset(, 1).Address
    End If
Next    
End Sub

如果您使用“表单”工具栏中的组合框,那么我不确定最好的方法是什么。我想尝试使用上面的方法。

Reima,

If you are using controls from the "Control Toolbox" toolbar, then the following should work. The only difference is that it's not an MSForms.Combobox. :

Sub Test()
Dim cbo As OLEObject

For Each cbo In ActiveSheet.OLEObjects
    If TypeOf cbo.Object Is ComboBox Then
        cbo.LinkedCell = cbo.TopLeftCell.Offset(, 1).Address
    End If
Next    
End Sub

If you are using a combobox from the "Forms" toolbar, then I'm not sure what the best approach is. I'd try using the method above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文