隐藏不同的列
我试图隐藏不相互隔离的不同列。
我有以下代码,但这仅允许我隐藏一个范围,例如在这种情况下列h:k
。如果我想隐藏其他列,请说我想隐藏h:k
,但s:t
,x:y
和列z:z
- 如何使以下代码用于非贴列列?
Option Explicit
'---------->>
Public Sub ToggleColumns()
Dim SH As Worksheet
Dim Rng As Range
Dim obj As Variant
Dim BTN As Button
Dim iLen As Long
Const myColumns As String = "H:K" '<<===== Change
Const sFees As String = "Fees" '<<===== Change
Const sHidden As String = " Hidden"
Const sVisible As String = " Visible"
Set SH = ActiveSheet
Set BTN = SH.Buttons(Application.Caller)
Set Rng = SH.Columns(myColumns)
With Rng.EntireColumn
.Hidden = Not .Hidden
If .Hidden Then
iLen = Len(sHidden) + Len(sFees)
BTN.Characters.Text = sFees & " Hidden"
With BTN.Characters(Start:=1, Length:=iLen).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.ColorIndex = 3 '\\ RED
End With
Else
iLen = Len(sVisible) + Len(sFees)
BTN.Characters.Text = sFees & " Visible"
With BTN.Characters(Start:=1, Length:=iLen).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.ColorIndex = 4 '\\ GREEN
End With
End If
End With
End Sub
I am trying to hide different columns that are not next to each other.
I have a the below code but this allows me to only hide a range, eg in this case columns H:K
. If I want to hide additional columns, say I want to hide H:K
but also S:T
, X:Y
and column Z:Z
- how do I get the below code to work for non-adjacent columns?
Option Explicit
'---------->>
Public Sub ToggleColumns()
Dim SH As Worksheet
Dim Rng As Range
Dim obj As Variant
Dim BTN As Button
Dim iLen As Long
Const myColumns As String = "H:K" '<<===== Change
Const sFees As String = "Fees" '<<===== Change
Const sHidden As String = " Hidden"
Const sVisible As String = " Visible"
Set SH = ActiveSheet
Set BTN = SH.Buttons(Application.Caller)
Set Rng = SH.Columns(myColumns)
With Rng.EntireColumn
.Hidden = Not .Hidden
If .Hidden Then
iLen = Len(sHidden) + Len(sFees)
BTN.Characters.Text = sFees & " Hidden"
With BTN.Characters(Start:=1, Length:=iLen).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.ColorIndex = 3 '\\ RED
End With
Else
iLen = Len(sVisible) + Len(sFees)
BTN.Characters.Text = sFees & " Visible"
With BTN.Characters(Start:=1, Length:=iLen).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.ColorIndex = 4 '\\ GREEN
End With
End If
End With
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换
为
columns
。
()不支持停产列的地址
Replace
with
and
with
because
Columns()
does not support addresses with discontinued columns.