+ Reply to Thread
Results 1 to 13 of 13

Macro to find blank cells, copy row, paste into another worksheet, then delete

  1. #1
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Macro to find blank cells, copy row, paste into another worksheet, then delete

    Hi,

    So I have 3 columns of data (Column A, B, C). Column C has some blank cells in it. I would like to be able to find all the blank cells in Column C, then copy the rows (Column A, B, C) which contain the blank cells, paste them into another sheet within the same workbook, then delete them from the original sheet.

    using the macro recorder I was able to do this:

    Sub Macro4()
    '
    ' Macro4 Macro
    '
    '
    Columns("C:C").Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete
    End Sub

    Obviously, this finds the cells which are blank & allows me to delete those rows containing the blank cells in the SAME worksheet. I still have not figured out the way to copy them to another sheet before deleting them.

    Is the current macro a good starting point to expand on, or should I try a totally different approach?

    Thanks in advance,
    Rob
    Last edited by rbrookov; 10-15-2013 at 10:05 AM. Reason: Marked as "SOLVED"

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Hi,

    Something like this:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Quote Originally Posted by xladept View Post
    Hi,

    Something like this:

    Please Login or Register  to view this content.

    xladept - wow, that works perfectly!! Thank you SOOOOO much!!

    A couple requests though, if you don't mind. I see that your code creates a new sheet - however, what if I already have a sheet, let's call it "NO_DATA" - what changes need to be made to paste the rows into a sheet which already exists?

    Also, for those of us learning VBA, can you explain what the commands do, perhaps w/ comments? For instance, why is there an equal sign (=) for Criteria1? What does that do?

    Anyway, I'm very appreciative that you were able to solve this issue - you don't know how much time this is going to save at work!!

    Thank you!

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Like this:

    Please Login or Register  to view this content.
    The "=" sign for Criteria1 tells the filter to look for "blanks"

    And, you're welcome!
    Last edited by xladept; 10-10-2013 at 12:11 AM.

  5. #5
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Quote Originally Posted by xladept View Post
    Like this:

    <SNIP CODE>

    The "=" sign for Criteria1 tells the filter to look for "blanks"

    And, you're welcome!
    xladept - so if I'm just using Excel formulas, the way I'd look for blanks is 2 quotation marks (ie, ""). So in VBA, the analogous symbol is the equal sign (ie, =)? OK, thanks for the clarification!!!

    Oh, and again, thank you for showing me how to do it 2 different ways: 1 way is to create a new sheet, and the other way is to add data to an existing sheet.

    I tried both, and both work as you had "advertised." :-) However, what about error checking? For instance, say I've ran the macro on a file, and it has blanks - thus, it will copy those rows & paste them to another sheet. However, if I run it again on another file & that file has no blanks, I will get an error - is there a way to have it give a message (perhaps to the screen) which says "no blank data" or something similar?

    Also, upon discussing the path forward w/ members of the team, it was suggested that since there will be some data files which do not contain blank data (some files do, some don't - it all depends on if the data collecting tool is up & running during the time the information is being logged to the file or not) - perhaps the original macro which creates a sheet if there are blank cells, then couple that with a display of a message saying "no blank data" if there are no blank cells? That seems like the best of both worlds, and will take into account files which do contain blank cells, as well as files which do not contain blank cells.

    I (and I know others who are curious about doing something similar to me) REALLY appreciate the help, and further appreciate the explanation of what is going on - perhaps one day, because of the knowledge folks like you are imparting to us, we can be here helping other "newbies" :-)

    THANK YOU!!
    Last edited by rbrookov; 10-10-2013 at 10:35 AM. Reason: clarify statement regarding error checking

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Two quotation marks actually signify a null field - the "blanks" category is both nulls and blanks - the ="=" only works with autofilter - keep using the two double quotes when looking for nulls

    I'll be looking into the "no blanks" situation anon.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Here's a routine that checks to see whether a filter is necessary and, if not, issues a message:

    Please Login or Register  to view this content.
    BTW - Do you want to stack the extracts rather than just write over them??
    Last edited by xladept; 10-10-2013 at 02:26 PM.

  8. #8
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Quote Originally Posted by xladept View Post
    Here's a routine that checks to see whether a filter is necessary and, if not, issues a message:

    <SNIP CODE>

    BTW - Do you want to stack the extracts rather than just write over them??
    xladept - hmmm, so I'm not sure if I'm doing something wrong, but it seems that the code now gives the message "No blank fields to filter" all the time, regardless as to whether there are blank fields or not in the data, and it is not making a new sheet called "NO_DATA" - it's basically just going straight to the display message of "No blank fields to filter" and doing nothing else.

    As far as the question about whether or not to "stack" the extracts (I'm assuming you mean append to existing data), or write them over - I would say that, if possible, what about looking to see if data already exists, and if not, then write the data to Cell A1, B1, C1 on a *new* sheet called "NO_DATA" but if data *does* already exist in the sheet called "NO_DATA" then just append the data to it.

    What I'm anticipating with this workbook is using it as a template whereby the original never gets modified. Instead, the user would make a copy of the template & rename it to something showing what date range they are processing. For instance, if they are processing from 10/01/13 - 10/07/13, then the filename for the Excel file would be something like "processed_data_10-01-13_10-07-13.xls"

    At this stage, it is still undetermined whether the user would copy the Excel template the next time he wishes to process data & rename it to, say, for example "processed_data_10-08-13_10-14-13.xls" or whether he would just use the same Excel file & append to it.

    Thus, if the code was "smart" enough to look to see if data already exists, that would work for either scenario.

    Again, much thanks!!

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    You may be running it without a "NO_DATA" sheet - try this:

    Please Login or Register  to view this content.

    BTW - It's expected that there is a sheet named NO_DATA in your workbook?

  10. #10
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Quote Originally Posted by xladept View Post
    You may be running it without a "NO_DATA" sheet - try this:

    <SNIP CODE>

    BTW - It's expected that there is a sheet named NO_DATA in your workbook?
    xladept - sorry, no. Perhaps I failed to explain, please allow me to try again. Originally, I was thinking that there would always be blank cells, and if that were the case, then it would make sense to always have a sheet in Excel named "NO_DATA." However, upon meeting & discussing w/ other members of the team (some of whom are more familiar w/ the data collection tool than I am), it was discovered that sometimes there will be files with NO blanks in them, and sometimes there will be.

    This revelation prompted a change in my thinking - which was to have the Excel file start off NOT having a sheet named "NO_DATA" then, upon running the macro on the data, if blanks DO exist, then create a sheet called "NO_DATA" and paste the rows into it. However, if blanks do NOT exist, just present the message to the screen which says "No blank fields to filter."

    Also, at this point, I'm not sure if the end-user is going to copy & paste, thus creating a new Excel file each time they run the macro, or if they are going to run it over & over on the same file. If they are going to run it over & over, then the next time, if blanks exist, the macro can append the new data to the end of the existing data & keep moving down the rows from there. If no blanks exist, then nothing will get written to the file.

    To me, that was the "best of both worlds" because it provided a solution for both instances (blanks, & no blanks), as well as allowed the user the choice to use a new Excel file each time, or keep using the "old" one.

    Hopefully I have cleared up any ambiguities this may have caused, and I apologize if I wasn't more clear earlier.

    Thank you.
    Last edited by rbrookov; 10-11-2013 at 11:53 AM.

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Try this version - it should create the output sheet if necessary or append to it if present:

    Please Login or Register  to view this content.
    Last edited by xladept; 10-11-2013 at 02:19 PM. Reason: Details

  12. #12
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    Quote Originally Posted by xladept View Post
    Try this version - it should create the output sheet if necessary or append to it if present:

    <SNIP CODE>
    xladept - this works BEAUTIFULLY!! Thank you very much for all your assistance!!

    I believe I may need to customize it a bit, as the team has decided they wanted to change some of the initial decisions / requirements. For instance, rather than create a new worksheet & paste the rows w/ blank cells there, they feel it would be better to paste those rows into an existing sheet, which already contains 3-4 columns of data, and just move the new stuff over to the side (say, starting at Column "G").

    So, I will work on customizing the code to do this (or if I get stuck, may seek your help again) hehe...

    Anyway, what you have shown me thus far has really helped increase my knowledge level in VBA, and I am now excited to take this & run with it!!

    THANK YOU!!

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to find blank cells, copy row, paste into another worksheet, then delete

    You're welcome! I'm especially pleased that you feel that you've learned from me

+ 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. [SOLVED] vba to copy non blank cells and paste to other worksheet
    By tantcu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-18-2013, 06:22 PM
  2. I am in deperate need of a macro to find blank cells in a column and delete the row
    By leon1233 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-05-2013, 05:57 PM
  3. [SOLVED] Copy row of data, paste in another worksheet, delete some of the cells copied
    By dev111ski in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-14-2012, 05:58 PM
  4. Copy and paste macro-blank cells thereafter.
    By Bikernick in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-04-2006, 12:00 PM
  5. Macro copy and paste = blank worksheet
    By efface in forum Excel General
    Replies: 1
    Last Post: 04-27-2006, 04:52 PM

Tags for this Thread

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