Consulting

Results 1 to 5 of 5

Thread: Solved: Help With Hiding Colums

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location

    Solved: Help With Hiding Colums

    I have the following sub to hide the last used column. I found that it does not work on the last worksheet and I can't figure out why. I need to hide the last used column on each worksheet and then I will need to unhide that column after some other procedures. Can you guys take a look and tell me where I went wrong.
    Thanks
    Gary

    [VBA]
    Sub HideColumns()
    Dim ws As Worksheet
    Dim LastColumn As Integer

    For Each ws In ThisWorkbook.Worksheets
    ws.Activate

    'Find The Last Column On The Worksheets
    LastColumn = Cells.SpecialCells(xlCellTypeLastCell).Column

    'Hide The Last Column
    Columns(LastColumn).Hidden = True

    Next ws
    End Sub
    [/VBA]

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Don't suppose you could provide a workbook, could you?

    I don't see anything wrong with the coding...a look at the book would be more useful.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I'm curious... does the code seem to hide the wrong column on the last one, or none at all? If it's the wrong column, you may want to delete all columns after the last one and try it again. It may be that you have a space or non printing character buried somewhere that you didn't expect.

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Gary,

    If you're wanting to hide the last data column, try [VBA]Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column[/VBA]the xlCellTypeLastCell uses the "UsedRange", and if the last column only has (say) some formatting in it then that is determined as being the last column that was 'used' even though it may contain no data...

    HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Ken you were right, I deleted the remaining columns and it worked thereafter.
    John I used your code, seems to work well.
    Malik, right now I am unable to post my file because I don't have a zip utility on my work computer and the file is too big.

    Thank you gentlemen for the help.
    Gary

Posting Permissions

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