OLEFormat及其在Excel工作表中的位置

0h4hbjxa  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(194)

我想创建一个简单的宏,只需检查复选框是否被选中,并基于此,隐藏或显示行。
但是有一些捕获,我不能将chackbox链接到单元格,否则另一个更大的宏会生成错误。
我研究了一下,发现你可以这样做:

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

是错误的,因为如果我运行宏,因为我张贴它,宏返回msgbox“嗨”,因为错误的部分被注解。
奇怪的是,如果我这样做:

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

它能让行...
我在想这个错误与OLEFormat.object或其他什么有关,但我的谷歌研究空手而归。

xytpbqjk

xytpbqjk1#

我会考虑抛弃复选框,用超链接来代替它们,这些超链接的风格就像复选框一样。这样就可以进行排序,复制和粘贴等,而不会有“漂移”的机会,复选框可以从它们分配的行中分离出来。
用于创建和支持链接的代码(在常规模块中)

'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

单击时切换链接外观(工作表代码模块):

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

相关问题