Select multiple columns to excel VBA

Advertisements
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. MyC = "G:I And "T:W" is clearly giving error. Please guide me how to select multiple columns

>Solution :

This should work, access the range first 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:

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

Leave a ReplyCancel reply