如何向 Excel 2010 中一列中的每个单元格添加是/否组合框

发布于 2024-09-06 15:14:56 字数 33 浏览 4 评论 0原文

见标题。如何在 Excel 2010 中执行此操作?

See title. How can I do this in Excel 2010?

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

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

发布评论

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

评论(4

樱花坊 2024-09-13 15:14:56

在 Excel 2007 的“数据”选项卡下,您可以在“数据验证”下找到它。

In Excel 2007 under the Data tab, you can find it under Data Validation.

盛装女皇 2024-09-13 15:14:56

Excel 中没有内置方法来生成链接到底层单元格的表单复选框负载。如果您复制单个复选框,它将具有所有相同的属性(包括链接的单元格),这意味着您每次都必须手动编辑它。为了一次添加一堆,您必须创建一个 VBA 函数来为您完成此操作。

值得庆幸的是 比我聪明的人 已经做到了。下面是一个这样的示例代码:

Option Explicit
Sub insertCheckboxes()

  Dim myBox As CheckBox
  Dim myCell As Range

  Dim cellRange As String
  Dim cboxLabel As String
  Dim linkedColumn As String

  cellRange = InputBox(Prompt:="Cell Range", _
    Title:="Cell Range")

  linkedColumn = InputBox(Prompt:="Linked Column", _
    Title:="Linked Column")

  cboxLabel = InputBox(Prompt:="Checkbox Label", _
    Title:="Checkbox Label")

  With ActiveSheet
    For Each myCell In .Range(cellRange).Cells
      With myCell
        Set myBox = .Parent.CheckBoxes.Add(Top:=.Top, _ 
          Width:=.Width, Left:=.Left, Height:=.Height)

        With myBox
          .LinkedCell = linkedColumn & myCell.Row
          .Caption = cboxLabel
          .Name = "checkbox_" & myCell.Address(0, 0)
        End With

        .NumberFormat = ";;;"
      End With

    Next myCell
  End With
End Sub

您应该将其复制到 VBA 模块中。按 Alt+F11 打开 VBA 编辑器,选择当前工作簿的模块,然后粘贴该代码。如果当前工作簿中没有模块,请右键单击工作簿名称并使用 Insert -> ; 新模块 添加一个。

返回工作簿并按 Alt-F8 运行宏。如果运行 insertCheckboxes 宏,您将看到以下对话框:

在此处输入范围

输入您想要在其中添加复选框的范围(例如,A1:A10)

在此处输入链接列

输入您想要在其中添加复选框的列链接到(如果您选择 B,则意味着 B 列将显示复选框的 TRUE/FALSE 结果)。

在此处输入复选框标签

在复选框上输入所需的标签。如果您只想要该框,请将其留空。

VBA 有几个小问题(例如,它将复选框所在的单元格设置为 ;;; 使它们不显示任何内容,可能是在您将复选框链接到它们所在的单元格的情况下)位于),但是一点创造性的工程应该可以帮助你解决这个问题。

使用宏后,您可以删除该模块——宏只是创建复选框,不需要维护它们。

注意:上面的代码已经过测试,并且可以在 Excel 2010 中运行。您的环境可能会有所不同

There is no built-in way in Excel to generate a load of Form Checkboxes linked to underlying cells. If you copy a single checkbox, it will have all the same properties (including linked cell) meaning you will have to edit it manually each time. In order to add a bunch at once, you have to create a VBA function to do it for you.

Thankfully people smarter than I have already done that. Here is one such example code:

Option Explicit
Sub insertCheckboxes()

  Dim myBox As CheckBox
  Dim myCell As Range

  Dim cellRange As String
  Dim cboxLabel As String
  Dim linkedColumn As String

  cellRange = InputBox(Prompt:="Cell Range", _
    Title:="Cell Range")

  linkedColumn = InputBox(Prompt:="Linked Column", _
    Title:="Linked Column")

  cboxLabel = InputBox(Prompt:="Checkbox Label", _
    Title:="Checkbox Label")

  With ActiveSheet
    For Each myCell In .Range(cellRange).Cells
      With myCell
        Set myBox = .Parent.CheckBoxes.Add(Top:=.Top, _ 
          Width:=.Width, Left:=.Left, Height:=.Height)

        With myBox
          .LinkedCell = linkedColumn & myCell.Row
          .Caption = cboxLabel
          .Name = "checkbox_" & myCell.Address(0, 0)
        End With

        .NumberFormat = ";;;"
      End With

    Next myCell
  End With
End Sub

You should copy this in to a VBA module. Hit Alt+F11 to open the VBA editor, select the module of your current workbook, and paste that code in. If there is no module in your current workbook, rightclick the workbook name and use Insert -> New Module to add one.

Return to your workbook and hit Alt-F8 to run a macro. If you run the insertCheckboxes macro, you will get the following dialogue:

Enter Range Here

Enter the range you want checkboxes in (for instance, A1:A10)

Enter Linked Column Here

Enter the column you want the checkboxes to be linked to (if you select B, that means column B will show the TRUE/FALSE result of the checkbox).

Enter the Checkbox Label Here

Enter the label you want on the checkboxes. If you want just the box, leave it blank.

There are a couple small issues with the VBA (for instance, it sets the cells the checkboxes go in to as ;;; making them display nothing, probably for cases where you link the checkboxes to the cells they are located in), but a little creative engineering should get you around that.

After you've used the macro, you can delete the module -- the macro just creates the checkboxes, it isn't needed to maintain them.

Note: The above code was tested and worked in Excel 2010 for me. Your environment may vary

鹿港巷口少年归 2024-09-13 15:14:56

我不确定 2010 年的界面,在 2003 年,您将转到“验证”对话框并添加列表检查。

在代码中是:

columns(1).validation.add xlValidateList,,,"yes,no"

I'm not sure about the 2010 interface, in 2003 you'd go to the Validation dialog and add a List check.

In code that'd be:

columns(1).validation.add xlValidateList,,,"yes,no"
哭泣的笑容 2024-09-13 15:14:56

这会对你有所帮助。这是一个视觉教程。它使用数据验证来形成组合框

http:// www.wikihow.com/Add-a-Drop-Down-Box-in-Excel-2007

This will help you. this is a visual tutorial. it uses data validation to form the combo box

http://www.wikihow.com/Add-a-Drop-Down-Box-in-Excel-2007

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