+ Reply to Thread
Results 1 to 9 of 9

Macro to Select and Delete Columns and or Rows

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Macro to Select and Delete Columns and or Rows

    Is there any way to use a macro to select a user defined column e.g. column S, together with all the columns to the right of that column and then have the Macro delete the selected columns. A key to this would be that it would need to cycle through every sheet in the workbook excluding the first say two or three sheets, again it needs to be possible to specify which sheets to exclude.

    It would be great if the same could be achieved for rows, though I don't know if that would need to be done via a second macro?

    Many thanks

  2. #2
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Macro to Select and Delete Columns and or Rows

    yes that is do-able
    to clarify you want to delete all sheets after (user input x), delete all rows after (user input y) on all sheets and delete all columns after (user input z) on all sheets?

    or

    you want to delete all rows after (user input x) and all columns after (user input y) on all sheets after (user input z)?

    Note: are these sheets always going to be in order of creation or will the sheet tabs have been shuffled about?

  3. #3
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Macro to Select and Delete Columns and or Rows

    Hi Leon V (AW),

    No sheets need to be deleted, it is only columns and/or rows on the existing sheets. I've attached a really basic sample.

    So, on the attached, when the macro is run, it would need to ignore the first two sheets, 'Main' and 'Instructions', so maybe the sheet names need to be specified manually in the macro by the user unless this there is a neater way to do this?

    Ideally the macro when run, say for Columns, would show a pop up box saying something like 'from which column do you wish to delete?', allowing the user to enter 'T' or 20 (in this example, as T is column 20), then when the user clicks OK, Column T and all the Columns to the right of it are selected and deleted.

    This needs to then cycle through all the sheets in the workbook, in the attached 6AX to 4FG (there will generally be around 200 per workbook), so in the sample attached, the macro would select all the green shaded columns, and delete them for each sheet with the execption the first two sheets, leaving just the yellow shaded columns and blank columns to the right.

    It would be he same process for the rows. It may be better to have these as two seperate macro's because the user may need to delete columns but no rows and vice versa.

    I hope that makes sense, if not, please let me know and I will try to explaion in more detail.

    Many thanks
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Macro to Select and Delete Columns and or Rows

    if there are 200 sheets they are unlikely to be in object order. selecting sheets to run on can be done using tab colour, sheet title or a userform listbox.
    the userform would look cleaner however tab colour would most likely be quickest to use. up to you.

  5. #5
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Macro to Select and Delete Columns and or Rows

    Would it not be easier, as there may only be one or two sheets the macro shouldn't be included on, to specify the exclusions, along the lines of:

    Please Login or Register  to view this content.
    rather than looking at tab colours etc, of which there may be numerous.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Macro to Select and Delete Columns and or Rows

    Perhaps.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  7. #7
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Macro to Select and Delete Columns and or Rows

    Hi Norie,

    That works perfectly and is a very elegant solution. I adapted it for Rows as well, many thanks... My only question is, how do I remove the run-time error 424 should the user click the cancel button on the userform instead of selecting the row or column?

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Macro to Select and Delete Columns and or Rows

    I've always avoided using Application.InputBox and now I know remember why.

    I actually thought the If statement would take care of the user clicking Cancel.

    Perhaps you could wrap that part of the code in On Error..., another thing I try to avoid but perhaps it's the only way to handle this.

    Aye, seems to work.
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Macro to Select and Delete Columns and or Rows

    Yep, that did the trick...

    Many thanks, that will save a lot of time... THanks for your quick and efficient response...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro help to search in all columns and delete rows accordingly
    By JonnyNYC90 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-20-2012, 03:44 PM
  2. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  3. Macro to select all rows and columns and add a new tab
    By Batman11692003 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-26-2011, 01:33 PM
  4. Locking columns containing formulas but being able to select & delete rows
    By bernborough in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2010, 03:34 AM
  5. [SOLVED] Macro to select 4 entire rows and then delete rows
    By Scott Viney in forum Excel General
    Replies: 3
    Last Post: 09-22-2005, 03:05 PM

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