+ Reply to Thread
Results 1 to 7 of 7

Macro for selecting colums XX from Start and XX from end

  1. #1
    Forum Contributor
    Join Date
    04-27-2010
    Location
    Kinsale, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    129

    Macro for selecting colums XX from Start and XX from end

    Dear Forum.
    I'm looking for a macro that would select a group of columns and hide them. I want the selection to be:
    the fourth column from the 1st column, up to the 3rd last column. And then hide them.
    Any help would be great.
    Thank you.
    Last edited by Cidona; 07-03-2011 at 12:58 PM. Reason: Marked as Solved

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Macro for selecting colums XX from Start and XX from end

    Try this, assuming you have headers in Row 1
    Please Login or Register  to view this content.

    You also might want to try this

    Please Login or Register  to view this content.
    Then use the resulting Grouping Button (-/+) to toggle the columns.

    Hope this helps
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Macro for selecting colums XX from Start and XX from end

    Hi Cidona,

    Find the attached with the Code you want.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Macro for selecting colums XX from Start and XX from end

    @ MarvinP
    Based on this
    I want the selection to be:
    the fourth column from the 1st column, up to the 3rd last column.
    I think that there could empty columns at the begining of the sheet otherwise Cidona would just have said...
    "I want the selection to be:
    From the fourth column up to the 3rd last column"

    I might well be wrong, but the code I posted handles either scenario.

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Macro for selecting colums XX from Start and XX from end

    Hi Marcol,

    After reading our code and seeing I started in Row 1 to find LastColumn, I wondered if we should have used UsedRange or some other method to find the working range s/he wanted.

    It is a mystery what the OP is really asking for in many of these questions. That is why I like to supply a workbook to show my assumptions (Row 1 is the column heads) to go along with the code. We'll see if they respond with a "you guys didn't get what I was saying" or if our answers are good enough for them.

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Macro for selecting colums XX from Start and XX from end

    Maybe this would be a more general solution?
    Please Login or Register  to view this content.

    Call the macro in "ToggleColumns" like this
    if the header row is row 1
    ShowHideColumns "from the 4th column" = 4 , "to the third last column" = 3
    Please Login or Register  to view this content.
    or if the header row is not row 1
    ShowHideColumns "from the 4th column" = 4 , "to the third last column" = 3, "the row number"
    e.g.
    Please Login or Register  to view this content.

    A similar approach could be used for the grouping button

    We'll see.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    04-27-2010
    Location
    Kinsale, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    129

    Re: Macro for selecting colums XX from Start and XX from end

    Thank you both so much for your replies and in depth anaylsis. And sorry I wasn't more specific. I was trying to keep it simple.
    I have ended up using MarvinP's solution. I'm sure all of the proposed methods could be tweeked to work for my situation but I just found Marvin's the simplest to edit. The only item I needed to change here was to change the 1 to a 7 in the line "LastC = Cells(1, Columns.Count).End(xlToLeft).Column 'Last Col in Row 1". This is because I am using the top rows of the worksheet as a location to put various buttons for activating macros. Since I am literally fumbling my way through the learning of macros I found this the easiest way.
    BTW Marcol, as far as "I think that there could empty columns at the begining of the sheet otherwise Cidona would just have said...
    "I want the selection to be:
    From the fourth column up to the 3rd last column"

    I might well be wrong, but the code I posted handles either scenario.

    I think that was what I was trying to say Sorry for the confusion.
    Thank you both again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1