英文:
Shape.OLEFormat and its position in the excel sheet
问题
我想创建一个简单的宏,只需检查复选框是否选中,根据情况隐藏或显示行。
但有一些问题,我不能将复选框链接到单元格,否则另一个更大的宏会生成错误。
我进行了一些研究,并发现可以使用:
sheets(1).shapes("Checkbox 88").topleftcell.row
来获取形状所在的行。
所以我试图将这个实现到我的代码中:
Dim sh As Shape
For Each sh In Sheets(1).Shapes
If TypeOf sh.OLEFormat.Object Is CheckBox Then
If sh.OLEFormat.Object.Value = -4146 Then
'sh.OLEFormat.Object.TopLeftCell.Row.EntireRow.Hidden = True
MsgBox "Hi"
End If
End If
Next sh
我知道:
sh.OLEFormat.Object.TopLeftCell.Row.EntireRow.Hidden = True
是错误的,因为如果我按照我发布的方式运行宏,它会返回消息框“Hi”,因为错误的部分被注释掉了。
对我来说奇怪的是,如果我这样做:
Dim aux As Byte
Dim sh As Shape
aux = Sheets(1).Shapes("Checkbox 88").OLEFormat.Object.TopLeftCell.Row
'checkbox 88是Excel文档中的一个复选框/形状
MsgBox aux
它可以获取到行...
我在想错误可能与 `OLEFormat.object` 或者其他东西有关,但我的谷歌搜索没有结果。
英文:
I wanted to create a simple macro that simply checked if a check box is checked or not and based on that, hide or show the row.
But there are some catches, I cannot link the chackbox to the cell, otherwise another bigger macro generates an error.
I did researcha bit and found that you can do:
sheets(1).shapes("Checkbox 88").topleftcell.row
to get the row of the shape.
So I tried to implement this to my code:
Dim sh As Shape
For Each sh In Sheets(1).Shapes
If TypeOf sh.OLEFormat.Object Is CheckBox Then
If sh.OLEFormat.Object.Value = -4146 Then
'sh.OLEFormat.Object.TopLeftCell.Row.EntireRow.Hidden = True
MsgBox "Hi"
End If
End If
Next sh
I know that the:
sh.OLEFormat.Object.TopLeftCell.Row.EntireRow.Hidden = True
is wrong, because if I run the macro as I posted it, the macro returns the msgbox "Hi", because the wrong part is commented.
The strange part for me is that if I do:
Dim aux As Byte
Dim sh As Shape
aux = Sheets(1).Shapes("Checkbox 88").OLEFormat.Object.TopLeftCell.Row
'checkbox 88 is one of the checkboxes/shapes in the excel document
MsgBox aux
it works to get the row...
I am thinking that the error has to do with the OLEFormat.object
or something, but my google researches came empty handed.
答案1
得分: 2
我会考虑放弃复选框,用样式化成复选框的超链接来替代它们。这样可以进行排序、复制、粘贴等操作,而不会出现复选框与其关联行分离的情况。
例如:用于创建和支持链接的代码(在普通模块中)
'在范围中的单元格中添加一些超链接“复选框”
Sub CreateCheckboxes()
Dim i As Long
For i = 1 To 20
AddCheckBox ActiveSheet.Cells(i, "B")
Next i
End Sub
'向单元格添加“复选框”并进行一些格式设置
Sub AddCheckBox(c As Range)
With c
.Hyperlinks.Delete
.Worksheet.Hyperlinks.Add anchor:=c, Address:="#LinkTarget()", _
TextToDisplay:=UncheckedValue
.Font.Name = "Wingdings" '此字体包含“复选框”字符
.Font.Size = 12 '去除默认的蓝色下划线格式
.Font.Underline = False
.Font.Color = vbBlack
.HorizontalAlignment = xlCenter
End With
End Sub
'这充当超链接的“虚拟”目标,只是返回所点击的单元格,以便选择不会跳转
Function LinkTarget() As Range
Set LinkTarget = Selection
End Function
'“已选中”单元格的文本
'空格只是为了扩展超链接的“可点击”区域
Function CheckedValue() As String
CheckedValue = " " & Chr(120) & " "
End Function
'“未选中”单元格的文本
Function UncheckedValue() As String
UncheckedValue = " " & Chr(111) & " "
End Function
'单元格是否“已选中”
Function IsChecked(c As Range) As Boolean
IsChecked = (c.Value = CheckedValue)
End Function
在点击时切换链接外观(工作表代码模块):
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim c As Range
Set c = Target.Range
If c.Column = 2 Then '如果需要根据单元格位置执行某些特定操作(例如),则可以进行某些特定操作
Debug.Print c.Address
Select Case c.Text '切换单元格文本
'如果需要在选中/取消选中复选框时执行某些特定操作,可以在下面添加代码
'此代码只切换复选框外观
Case CheckedValue: c.Value = UncheckedValue
Case UncheckedValue: c.Value = CheckedValue
Case Else: c.Value = UncheckedValue
End Select
End If
End Sub
英文:
I would think about ditching checkboxes and replacing them with hyperlinks styled to act like checkboxes. This allows sorting, copying and pasting, etc with no chance of "drift" where checkboxes can become detached from their assigned row.
Eg: code for creating and supporting links (in a regular module)
'add some hyperlink "checkboxes" to cell in a range
Sub CreateCheckboxes()
Dim i As Long
For i = 1 To 20
AddCheckBox ActiveSheet.Cells(i, "B")
Next i
End Sub
'Add a "checkbox" to a cell and do some formatting
Sub AddCheckBox(c As Range)
With c
.Hyperlinks.Delete
.Worksheet.Hyperlinks.Add anchor:=c, Address:="#LinkTarget()", _
TextToDisplay:=UncheckedValue
.Font.Name = "Wingdings" 'this font has the "checkbox" characters
.Font.Size = 12 'remove the default blue+underline formatting
.Font.Underline = False
.Font.Color = vbBlack
.HorizontalAlignment = xlCenter
End With
End Sub
'This serves as a "dummy" destination for the hyperlinks
' Just returns the clicked-on cell, so the selection doesn't jump around
Function LinkTarget() As Range
Set LinkTarget = Selection
End Function
'Text for a "checked" cell
'Spaces are just to expand the "clickable" area of the hyperlink
Function CheckedValue() As String
CheckedValue = " " & Chr(120) & " "
End Function
'Text for an "unchecked" cell
Function UncheckedValue() As String
UncheckedValue = " " & Chr(111) & " "
End Function
'Is a cell "checked"
Function IsChecked(c As Range) As Boolean
IsChecked = (c.Value = CheckedValue)
End Function
Switch link appearance on click (worksheet code module):
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim c As Range
Set c = Target.Range
If c.Column = 2 Then 'if (eg) you need to take some specific action based on where the cell is
Debug.Print c.Address
Select Case c.Text 'Toggle cell text
'You can add code below if some specific action
' to be taken when a checkbox is checked/unchecked.
'This code just toggles the checkbox appearance
Case CheckedValue: c.Value = UncheckedValue
Case UncheckedValue: c.Value = CheckedValue
Case Else: c.Value = UncheckedValue
End Select
End If
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论