简体   繁体   English

Select 多列到 excel VBA

[英]Select multiple columns to excel VBA

Private Sub ShowHideWST_Click()
Dim MyC As String
MyC = "G:I" And "T:W"
If ShowHideWST.Value Then
Application.ActiveSheet.Columns(MyC).Hidden = True
Else
Application.ActiveSheet.Columns(MyC).Hidden = False
End If
End Sub

I want to select multiple column in excel VBA.我想在 excel VBA 中 select 多列。 MyC = "G:I And "T:W" is clearly giving error. Please guide me how to select multiple columns MyC = "G:I And "T:W"显然给出错误。请指导我如何 select 多列

This should work, access the range first (Provide the range in A1 -style, try doing the action with the macro recorder and it would have shown you the range reference) then to its Columns property to set the Hidden property, the code can be shorten as well since you are setting the Hidden value to be the same as ShowHideWST.Value :这应该可以,首先访问范围(以A1样式提供范围,尝试使用宏记录器执行操作,它会向您显示范围参考)然后对其Columns属性设置Hidden属性,代码可以是缩短,因为您将Hidden值设置为与ShowHideWST.Value相同:

Private Sub ShowHideWST_Click()
    Const MyC As String = "G:I,T:W"
    
    ActiveSheet.Range(MyC).Columns.Hidden = ShowHideWST.Value
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM