+ Reply to Thread
Results 1 to 8 of 8

"Batch" deleting columns

  1. #1
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    "Batch" deleting columns

    So I have a series of workbooks that I need to make a series of (very simple) alterations to.

    Foremost, I need to cycle through a series of workbooks all found in the same folder regardless of their names. In other words, it has to hit every workbook I put in a folder. While they will have a standard naming structure, I don't want to rely on it.
    As its cycling through these workbooks, it should do three things. The first is "identify" the sheet I care about. That is simply defined by any sheets not named either "MTD" or "Mozart Reports." Once we're working on that sheet (it will always only be one sheet,) we have two tasks left:

    1) Insert the title of the workbook into Cell A1 (always A1, so if the workbook is titled "Report 12/21/12" then Cell A1 gets overwritten with "Report 12/21/12." Is this possible?
    2) I need to delete a series of several entire columns. Not just the contents, but the entire columns. In my current example, this means deleting columns: E, F, J, K, L, M, N, O, P, Q, R, S, T, U, V, X, Y, Z, AA, AB, AD.

    After that, we're done

    So in brief I just need to open a series of workbooks, populate cell A1 from all of them with the filename of the workbook, and delete a series of columns. The columns I need deleted in particular can be named if necessary, but if its left modular I can change it on the fly that's even better.

    Please let me know if you need a sample workbook.
    Last edited by CapnSef; 01-18-2013 at 01:40 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: "Batch" deleting columns

    Hi,

    I think this code does what you want. Make sure to save your files in another folder in case something goes wrong. The macro will save the files after deleting columns so you may lose data!
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: "Batch" deleting columns

    RHCPgergo,

    Your code runs without error - but unfortunately it seems as though it isn't executing. I've run through it line by line, and it seems logical enough but for some reason or another (potentially my oversight) it isn't deleting any columns or overwriting data into cell A1.

    I've attached a handful of the reports I'm running this on in order to help illustrate the issue. Additionally, your code doesn't have to prompt me for the folder - it can be assumed I'm running this code on a file already in the folder I'm working on (did that make sense?)
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: "Batch" deleting columns

    I've tried it and it works for me. Maybe you put the excel file that contains the VBA code in the same folder as the other files. That could be a problem.

    I'm off now, but I'll reply some other time if it's still not solved. I don't have time to rewrite the code now. Godspeed!

  5. #5
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: "Batch" deleting columns

    Please Login or Register  to view this content.
    So I used the "working" part of your code in some code I already had from another great user on my forum and things are working wonderfully now. The one issue is it seems to not know when to stop deleting, and ends up deleting everything from E onward repeatedly until I'm only left with a few columns of data. The rename works great, however. I think I must have messed the loop up as I chopped the code together. Can you see where I went wrong? Thank you again for all your effort!

  6. #6
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: "Batch" deleting columns

    Not sure, but I have a few ideas.

    - You didn't declare the ws variable as a workbook. You're not using Option Explicit, so the ws variable is considered to be a variant. It's not a big problem though, but it is better practice to declare the variables you use. So
    Please Login or Register  to view this content.
    should be added.

    Please Login or Register  to view this content.
    should be
    Please Login or Register  to view this content.
    ThisWorkbook means the workbook in which you store the code.

    I'm not sure about this one.
    Please Login or Register  to view this content.
    Shouldn't it just be
    Please Login or Register  to view this content.
    ws.Close False closes the workbook without saving.

    Ok that's all of my ideas for now, please test them. Please note that my original code works for me if the excel file containing the macro is not in the destination folder. Have you tried that? But the new code should also work if it's corrected.

  7. #7
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: "Batch" deleting columns

    The c01 variable also wasn't declared. Then again, it should work without declaring.

  8. #8
    Registered User
    Join Date
    01-17-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: "Batch" deleting columns

    Thanks for the all the help ! I got it running close enough Marked as solved and repped!

+ 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