Excel - 如何使用 VBA 添加复选框并链接到它旁边的单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14722444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel - How to add checkbox with VBA and linking to cell next to it?
提问by Punchlinern
I'm writing a schedule in Excel with a lot of conditional formatting which alerts me if a person gets overlapping tasks. If someone doesn't show up someone else has to take several spots at the same day, then I want to add that to the spreadsheet to remember that.
我正在用 Excel 编写一个带有很多条件格式的日程表,如果有人收到重叠的任务,它会提醒我。如果有人没有出现,其他人必须在同一天占据多个位置,那么我想将其添加到电子表格中以记住这一点。
The problem is that if I do so I get red cells alerting me, and I don't want that for passed dates. I thought that I might add a column called "ignoreErrors", or something like that, with checkboxes and if I check any of these Excel ignores the rest of the formatting rules.
问题是,如果我这样做,我会收到红色细胞提醒我,而我不希望在过去的日期出现这种情况。我想我可能会添加一个名为“ignoreErrors”或类似的列,带有复选框,如果我选中这些 Excel 中的任何一个,则会忽略其余的格式规则。
I found the following script here(I edited a little bit):
我在这里找到了以下脚本(我编辑了一点):
Sub AddCheckBoxes()
On Error Resume Next
Dim c As Range, myRange As Range
Set myRange = Selection
For Each c In myRange.Cells
ActiveSheet.checkboxes.Add(c.Left, c.Top, c.Width, c.Height).Select
With Selection
.LinkedCell = c.Address
.Characters.Text = ""
.Name = c.Address
End With
c.Select
Next
myRange.Select
End Sub
When I add the checkboxes I get a range of checkboxes, but with the cell value (TRUE or FALSE) in the background. I don't want that. I thought I would link the checkboxes to a cell in the column next to it, and hide that. Is that possible?
当我添加复选框时,我会得到一系列复选框,但背景中有单元格值(TRUE 或 FALSE)。我不想要那个。我想我会将复选框链接到它旁边列中的一个单元格,然后隐藏它。那可能吗?
回答by Bharath Raja
If you delete the line (or comment it)
如果您删除该行(或对其进行评论)
.LinkedCell = c.Address
it will not show the True or false.
它不会显示真假。
To link it to the next column, do the following
要将其链接到下一列,请执行以下操作
.LinkedCell = c.Offset(0, 1).Address
Hope this helps.
希望这可以帮助。
回答by Mark A Dvorak
I just make the font color in the linked cell white so you cant see the true/false.
我只是将链接单元格中的字体颜色设为白色,这样您就看不到真/假。
Simple but effective :)
简单但有效:)