Macro hide inconsistent selected columns in selected worksheets

K0st4din

Well-known Member
Joined
Feb 8, 2012
Messages
501
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello everyone, after quite a bit of searching around the site I found a query similar to my case and found an almost 100% solution with this macro.
However, in my case, could you please help me how on the line that says exactly which columns to hide, I write odd columns, for example B:B, K:K, O:O, Z:Z and etc. until the end, to write the desired columns.
Thanks in advance!

VBA Code:
Sub Hide_Columns()
Application.ScreenUpdating = False
On Error GoTo M
Dim Del As Variant
Del = Array("BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM", "NN")
ans = UBound(Del)
    For i = 1 To ans + 1
        Sheets(Del(i - 1)).Columns("F:AN").Hidden = True 
    Next
Exit Sub
M:
MsgBox "No such sheet exist"
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Trying to keep it similar to what you already have, try this:

VBA Code:
Sub Hide_Columns()
    Application.ScreenUpdating = False
    On Error GoTo ErrMsg
    Dim arrSht As Variant
    Dim arrCol As Variant
    Dim i As Long, j As Long
   
    arrSht = Array("BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM", "NN")
    arrCol = Array("B", "K", "O", "Z")
   
    For i = 0 To UBound(arrSht)
        With Sheets(arrSht(i))
            For j = 0 To UBound(arrCol)
                .Columns(arrCol(j)).Hidden = True
            Next j
        End With
    Next
    GoTo ExitSub
   
ExitSub:
    Application.ScreenUpdating = True
    Exit Sub

ErrMsg:
    MsgBox "No such sheet exist"
    Resume ExitSub
End Sub
 
Upvote 1
Trying to keep it similar to what you already have, try this:

VBA Code:
Sub Hide_Columns()
    ...................................

ErrMsg:
    MsgBox "No such sheet exist" To be "Missing DD sheet! or Missing GG, KK ect. sheets
Thank you so much.
That's exactly what I didn't know how to mention which columns exactly.
Postscript: And maybe When (if the inscription, That some worksheet/sheets are missing) is displayed.
I mean: MsgBox "No such sheet exist" - to be for example: You are missing - the name or names of the worksheets!
 
Upvote 0
Another option:

how on the line that says exactly which columns to hide, I write odd columns, for example B:B, K:K, O:O, Z:Z
You can use Range & EntireColumn instead of Columns to get non-contiguous columns:
VBA Code:
    For i = 1 To ans + 1
        Sheets(Del(i - 1)).Range("B1,K1,O1,Z1").EntireColumn.Hidden = True
    Next
 
Upvote 1
Postscript: And maybe When (if the inscription, That some worksheet/sheets are missing) is displayed.
I mean: MsgBox "No such sheet exist" - to be for example: You are missing - the name or names of the worksheets!
I have modified the code for your Postscript.

PS: @Akuini has given you a good option if you want to replace the inner loop

VBA Code:
Sub Hide_Columns()
    Application.ScreenUpdating = False

    Dim arrSht As Variant
    Dim arrCol As Variant
    Dim sMissSht As String
    Dim i As Long, j As Long
    
    arrSht = Array("BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM", "NN")
    arrCol = Array("B", "K", "O", "Z")
     
    For i = 0 To UBound(arrSht)
        If Evaluate("isref('" & arrSht(i) & "'!A1)") Then
           With Sheets(arrSht(i))
                   For j = 0 To UBound(arrCol)
                       .Columns(arrCol(j)).Hidden = True
                   Next j
           End With
        Else
            sMissSht = sMissSht & "; " & arrSht(i)
        End If
    Next
    If sMissSht <> "" Then
        sMissSht = Right(sMissSht, Len(sMissSht) - 2)
    Else
        MsgBox "Update completed"
    End If
    
    If sMissSht <> "" Then
        MsgBox "You are missing the worksheets - " & sMissSht
    End If
    
ExitSub:
    Application.ScreenUpdating = True

End Sub
 
Upvote 1
Solution
Hello Alex Blakenburg
really gave a good solution but i plan to use your last one.
Thank you very much for your quick help!
Be alive and healthy!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top