Unhide 'X' Amount of Columns from last hidden

Wizerd

New Member
Joined
Aug 11, 2017
Messages
28
G'day all,

Looking to see if I can get some help on unhiding a certain range of columns from the left of the active cell? I have months of column data and generally it's hidden up to current date. I was looking to have the user prompted for 'X' number of columns they need to unhide from the last hidden column.

Related would be a hide all columns to the left of the active cell when completed.

Any help would be appreciated
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this code:
VBA Code:
Sub MyUnhideMacro()

    Dim c1 As Long, c2 As Long
    
'   Capture column of active cell
    c1 = ActiveCell.Column
    
'   Prompt user for how many columns to left to unhide
    On Error GoTo err_fix
    c2 = InputBox("How many columns to the left would you like to unhide?")
    
'   Check to make sure number is at least 1 or greater
    If c2 < 1 Then
        MsgBox "Your entry must be 1 or greater!", vbOKOnly, "PLEASE TRY AGAIN!"
        Exit Sub
    End If
    
'   Check to see if number of columns to unhide makes sense
    If c2 > (c1 - 1) Then
        MsgBox "There are only " & c1 - 1 & " columns to the left of the active cell!", vbOKOnly, "PLEASE TRY AGAIN!"
        Exit Sub
    End If

'   Unhide columns
    Range(Cells(1, c1 - c2), Cells(1, c1 - 1)).EntireColumn.Hidden = False

        
    Exit Sub
    
'Error handling code
err_fix:
    MsgBox "Operation cancelled or invalid entry", vbOKOnly, "PLEASE TRY AGAIN!"
    
End Sub

Creating one to re-hide columns would be very similar. The big difference is you would use:
Rich (BB code):
.EntireColumn.Hidden = True
instead of
Rich (BB code):
.EntireColumn.Hidden = False
 
Upvote 0
Try:
VBA Code:
Sub UnhideColumns()
    Application.ScreenUpdating = False
    Dim x As Long, cnt As Long, col As Long, response As String
    response = InputBox("please enter the number of columns to unhide.")
    If response = "" Then Exit Sub
    col = Cells.SpecialCells(xlVisible).Cells(1).Column
    For x = col To 1 Step -1
        cnt = cnt + 1
        If cnt > response + 1 Then Exit Sub
        Columns(x).Hidden = False
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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