+ Reply to Thread
Results 1 to 10 of 10

Delete Multiple Columns with specific column headings using a Macro.

  1. #1
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Delete Multiple Columns with specific column headings using a Macro.

    Dear Forum,

    I need a little help in deleting a Few Columns which come in my Daily Dump...

    Ex: I have almost 59-60 Column Size Range of Data in which I need to delete a few columns as these are less relevant for my daily report.

    Manually Deleting them is tedious as this is a Daily Report and so if something can be done with a VBA macro this will a boon for me.

    There's also one catch as the Column Headings sometimes come differently like so please add an OR to the condition if possible.

    Ex: - I need to delete the 2 columns whose Column Headings are "Hub Code" and "Log PPT" or sometimes this may come as "HubCode" and "LogPPT"..

    So the columns with these column headings get deleted and the other column takes its place.

    Regards
    e4excel

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

    Re: Delete Multiple Columns with specific column headings using a Macro.

    Hi e4excel,

    See if this will work for you:
    Please Login or Register  to view this content.
    Last edited by MarvinP; 11-30-2013 at 07:17 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    11-05-2013
    Location
    Germany
    MS-Off Ver
    Excel 2013
    Posts
    120

    Re: Delete Multiple Columns with specific column headings using a Macro.

    Argh. I was too slow again ^^


    Anyways. Here is what I did

    Please Login or Register  to view this content.

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

    Re: Delete Multiple Columns with specific column headings using a Macro.

    Sorry I was too quick.

    There is a problem when deleting rows or columns and counting UP. If two rows or columns next to each other need to be deleted then your type of code will miss the second one. This is why it is better to start at the last one and move up or to the left. This way the counter from top to bottom works.

    I hope this makes sense and helps you have a better scheme next time.

  5. #5
    Forum Contributor
    Join Date
    11-05-2013
    Location
    Germany
    MS-Off Ver
    Excel 2013
    Posts
    120

    Re: Delete Multiple Columns with specific column headings using a Macro.

    Thank you!

    Very good to know.

  6. #6
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Delete Multiple Columns with specific column headings using a Macro.

    Dear MarvinP,

    For some strange Im getting an error in this line "LastCol = Cells(1, Column.Count).End(xlToLeft).Column" as Column is not a defined variable then just with fluke I tried it with Columns..it worked but still it does not move both the columns to the left..

    Please Login or Register  to view this content.
    Added this line from Decars code.. "Shift:=xlToLeft"

    but still though both columns got deleted , one of the columns just remained blank..


    Regards
    e4excel

  7. #7
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Delete Multiple Columns with specific column headings using a Macro.

    Hi,

    Try this :


    Please Login or Register  to view this content.
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  8. #8
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Delete Multiple Columns with specific column headings using a Macro.

    Thanks xlbzines and sorry Marvin I just realised that there was a small piece of code which was actually inserting a blank column instead of Cutinng and Inserting the column.

    Just a small question based on the same thing how do i Cut specific columns & insert the columns before or after specific columns?

    Thanks everyone for the help...
    Last edited by e4excel; 11-30-2013 at 05:55 AM.

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

    Re: Delete Multiple Columns with specific column headings using a Macro.

    My Bad,

    It should have been Columns.Count (with an S) in the line of:
    LastCol = Cells(1, Column.Count).End(xlToLeft).Column

    In the immediate window type "?Columns.Count" and press enter. It will show the max number of columns in your worksheet. It is different in 2003 vs 2007 (vs 2010??) and above Excel.

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

    Re: Delete Multiple Columns with specific column headings using a Macro.

    Hi e4excel,

    To do this extra step here is what I'd do....

    I'd record a macro to insert a column and then cut and copy another column into the blank one I'd inserted (If I understand what you are trying to do). Then I'd stop recording and look at the code the record produced. Then you patch it up with code that uses Cells(row, col) type of notation.....

    I can't tell you how many times I've learned VBA by recording a macro...

    I hope that helps you figure out how to insert columns and paste others into the blank one you just created..

+ 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 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
  2. Delete rows with specific value in multiple Columns
    By dagindi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-20-2010, 04:59 PM
  3. Delete multiple columns by column name no criteria need, just delete them
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2009, 10:40 AM
  4. delete specific columns by reading headings
    By sa02000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2008, 06:52 AM
  5. Replies: 5
    Last Post: 08-16-2006, 02: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