ashik4u
ashik4u

Reputation: 3

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

Upvotes: 0

Views: 316

Answers (1)

Raymond Wu
Raymond Wu

Reputation: 3387

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

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

Upvotes: 1

Related Questions