簡體   English   中英

Excel中的復選框

[英]Check Boxes in Excel

我正在嘗試插入多個復選框(100+),但不想單獨添加它們。 有快速的方法嗎? 我嘗試過下拉方法,它的確可以做更多,但是它們都連接在一起-復制和粘貼也是如此! 當單擊1時,它們都是!

單擊復選框時觸發的宏如下,我想知道是否可以更改?

Sub tick()

Dim ws As Worksheet
Dim chk As CheckBox
Dim lColD As Long
Dim lColChk As Long
Dim lRow As Long
Dim rngD As Range
lColD = 3 'number of columns to the right for date

Set ws = ActiveSheet
Set chk = ws.CheckBoxes(Application.Caller)
lRow = chk.TopLeftCell.Row
lColChk = chk.TopLeftCell.Column
Set rngD = ws.Cells(lRow, lColChk + lColD)

Select Case chk.Value
   Case 1   'box is checked
      rngD.Value = Date
   Case Else   'box is not checked
      rngD.ClearContents
End Select

End Sub

這是我經常使用的代碼。 如果您選擇一個范圍並運行此宏-它將為每個選定的單元格添加一個復選框,並將其與該單元格值綁定(布爾值)

我不使用字幕-但您可以根據需要輕松地對其進行修改。

我希望這個對你有用

Sub Add_checkboxes()


Application.ScreenUpdating = False

    For Each the_cell In Selection
    ActiveSheet.CheckBoxes.Add(Range(the_cell.Address).Left, Range(the_cell.Address).top, 18, 12).Select
    With Selection
        .Caption = ""
        .Value = xlOff '
        .LinkedCell = the_cell.Address
        .Display3DShading = False
    End With
    Next

Application.ScreenUpdating = True

End Sub

將此代碼添加到VBA中的Sheet-Module中,並使用“ Selection”(僅作為示例)標記要用作復選框的列。 雙擊時,此過程用“ X”標記指定的單元格

Private Sub Worksheet_beforedoubleClick(ByVal Target As Range, Cancel As Boolean)
    'If row 6 of a cell contains "Selection" all rows below are used as checkboxes.    
    If Me.Cells(6, Target.Column) = "Selection" And Target.Row > 6 Then

    'Function to mark the field with an "x"

          If Target.Value = "x" Then
              Target.Value = ""
          Else
              Target.Value = "x"
          End If

    End If
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM