好的,所以我使用 ActiveX 或表单控件得到相同的结果。无论出于何种原因,控件的原始高度似乎在保存和关闭之后 不会持续存在。
另一种选择是简单地清除工作簿Close
和Save
事件上的自动筛选。但是,如果您希望在保存并重新打开文件时保留一些过滤器,这可能不是您想要的。可能可以将过滤器参数保存在隐藏表中或通过直接操作 VBE/VBA 来保存,但这似乎比它的价值要麻烦得多。然后,您可以在重新打开工作簿时重新应用过滤器。
这是我建议的代码
注意:我依靠_Calculate
带有隐藏CountA
公式的工作表事件(设置、更改或清除自动筛选器将触发此事件)。我把公式放在 E1 中,这样你就可以看到它的样子:
由于您的应用程序依赖于这种方法,Calculation = xlManual
因此这种方法并不完全适合您,但无论如何,子例程UpdateButtons
都可以重复使用。您需要根据需要将其绑定到应用程序中的另一个事件或函数。
这是代码
Option Explicit
Private Sub UpdateButtons()
'## Assumes one button/shape in each row
' buttons are named/indexed correctly and
' the first button appears in A2
Dim rng As Range
Dim shp As Shape
Dim i As Long
Application.EnableEvents = False
'## use this to define the range of your filtered table
Set rng = Range("A1:A6")
'## Iterate the cells, I figure maybe do this backwards but not sure
' if that would really make a difference.
For i = rng.Rows.Count To 2 Step -1
Set shp = Nothing
On Error Resume Next
Set shp = Me.Shapes(i - 1)
On Error GoTo 0
If Not shp Is Nothing Then
DisplayButton Me.Shapes(i - 1), Range("A" & i)
End If
Next
Application.EnableEvents = True
End Sub
Private Sub DisplayButton(shp As Shape, r As Range)
'# This subroutine manipulates the shape's size & location
shp.Top = r.Top
shp.TopLeftCell = r.Address
shp.Height = r.Height
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "_Change"
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
''## Assumes one button/shape in each row
'' buttons are named/indexed correctly and
'' the first button appears in A2
'Dim rng As Range
'Dim shp As Shape
'Dim i As Long
'
''## Uncomment this line if you want an annoying message every time
''MsgBox "Refreshing Command Buttons!"
'
'Application.EnableEvents = False
''## use this to define the range of your filtered table
'Set rng = Range("A1:A6")
'
''## Iterate the cells, I figure maybe do this backwards but not sure
'' if that would really make a difference.
'For i = rng.Rows.Count To 2 Step -1
' Set shp = Nothing
' On Error Resume Next
' Set shp = Me.Shapes(i - 1)
' On Error GoTo 0
'
' If Not shp Is Nothing Then
' DisplayButton Me.Shapes(i - 1), Range("A" & i)
' End If
'Next
'
'Application.EnableEvents = True
End Sub
对于另一种选择,请参阅这篇文章。您可以通过 RibbonXML 自定义重新利用现有命令。虽然本文面向 C# 和 Visual Studio,但可以使用 CustomUI 编辑器来完成。