开发者

How to add yes/no combo box to the each cells in one column in Excel 2010

开发者 https://www.devze.com 2023-01-04 10:12 出处:网络
See title. How can I 开发者_如何学JAVAdo this in Excel 2010?In Excel 2007 under the Data tab, you can find it under Data Validation.There is no built-in way in Excel to generate a load of Form Checkbo

See title. How can I 开发者_如何学JAVAdo this in Excel 2010?


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


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:

How to add yes/no combo box to the each cells in one column in Excel 2010

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

How to add yes/no combo box to the each cells in one column in Excel 2010

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).

How to add yes/no combo box to the each cells in one column in Excel 2010

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


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"


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号