Home > OS >  Select multiple columns to excel VBA
Select multiple columns to excel VBA

Time:11-30

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

CodePudding user response:

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
  • Related