Consulting

Results 1 to 8 of 8

Thread: Hide Rows and Columns

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Location
    Texas
    Posts
    93
    Location

    Hide Rows and Columns

    Hello Everyone,

    I'm having trouble hiding row and columns on a spreadsheet. I would like to hide the rows and columns around the range a1:x43. Here is the code I'm using with little success:
    [VBA]Option Explicit
    Sub HideRowsAndColumns()
    Dim row1 As Long, row2 As Long
    Dim col1 As Long, col2 As Long

    If TypeName(Selection) <> "Range" Then Exit Sub

    ' If last row or last column is hidden, unhide all and quit
    If Rows(Rows.Count).EntireRow.Hidden Or Columns(Columns.Count).EntireColumn.Hidden Then
    Cells.EntireColumn.Hidden = False
    Cells.EntireRow.Hidden = False
    Exit Sub
    End If

    row1 = Selection.Rows(1).Row
    row2 = row1 + Selection.Rows.Count - 1
    col1 = Selection.Columns(1).Column
    col2 = col1 + Selection.Columns.Count - 1

    Application.ScreenUpdating = False
    On Error Resume Next
    ' Hide rows
    Range(Cells(1, 1), Cells(row1 - 1, 1)).EntireRow.Hidden = True
    Range(Cells(row2 + 1, 1), Cells(Rows.Count, 1)).EntireRow.Hidden = True
    ' Hide columns
    Range(Cells(1, 1), Cells(1, col1 - 1)).EntireColumn.Hidden = True
    Range(Cells(1, col2 + 1), Cells(1, Columns.Count)).EntireColumn.Hidden = True
    End Sub[/VBA]

    I selected the range but nothing happens. Any ideas?
    Thank you!
    Last edited by Bob Phillips; 11-11-2010 at 03:05 PM. Reason: Added VBA Tags

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If A1:X43 is selected, which rows and columns do you want hidden?

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is this what you mean?

    [vba]

    Sub HideRowsAndColumns()
    Dim row1 As Long, row2 As Long
    Dim col1 As Long, col2 As Long

    If TypeName(Selection) <> "Range" Then Exit Sub

    ' If last row or last column is hidden, unhide all and quit
    If Rows(Rows.Count).EntireRow.Hidden Or Columns(Columns.Count).EntireColumn.Hidden Then
    Cells.EntireColumn.Hidden = False
    Cells.EntireRow.Hidden = False
    Exit Sub
    End If

    row1 = Selection.Rows(1).Row
    row2 = row1 + Selection.Rows.Count - 1
    col1 = Selection.Columns(1).Column
    col2 = col1 + Selection.Columns.Count - 1

    Application.ScreenUpdating = False
    On Error Resume Next
    ' Hide rows
    Rows(row1).Resize(row2 - row1 + 1).Hidden = True
    ' Hide columns
    Columns(col1).Resize(, col2 - col1 + 1).Hidden = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Oct 2010
    Location
    Texas
    Posts
    93
    Location
    I would like to hide everything except the range selected.
    Thank you.

  5. #5
    VBAX Regular
    Joined
    Oct 2010
    Location
    Texas
    Posts
    93
    Location
    Thank you, but I want to hide everything but the range a1:x43.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub HideRowsAndColumns()
    Dim row1 As Long, row2 As Long
    Dim col1 As Long, col2 As Long

    If TypeName(Selection) <> "Range" Then Exit Sub

    ' If last row or last column is hidden, unhide all and quit
    If Rows(Rows.Count).EntireRow.Hidden Or Columns(Columns.Count).EntireColumn.Hidden Then
    Cells.EntireColumn.Hidden = False
    Cells.EntireRow.Hidden = False
    Exit Sub
    End If

    row1 = Selection.Rows(1).Row
    row2 = row1 + Selection.Rows.Count - 1
    col1 = Selection.Columns(1).Column
    col2 = col1 + Selection.Columns.Count - 1

    Application.ScreenUpdating = False
    On Error Resume Next
    ' Hide rows
    Rows(1).Resize(row1 - 1).Hidden = True
    Rows(row2 + 1).Resize(Rows.Count - row2).Hidden = True
    ' Hide columns
    Columns(1).Resize(, col1 - 1).Hidden = True
    Columns(col2 + 1).Resize(, Columns.Count - col2).Hidden = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Oct 2010
    Location
    Texas
    Posts
    93
    Location
    Thanks for all your help, but this isn't working. I'll find another way.
    Thank you again for all your effort!! I really appreciate you time!!

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Your original code works for me. What happens when you run it?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •