+ Reply to Thread
Results 1 to 9 of 9

Macro that can cut paste rows, when values are blanks or only show two values in a row

  1. #1
    Registered User
    Join Date
    01-12-2016
    Location
    Europe
    MS-Off Ver
    MS 2013
    Posts
    48

    Macro that can cut paste rows, when values are blanks or only show two values in a row

    Hi Forum,

    I am trying to build a macro that can cut paste Rows into sheet2, when values from column H to CS, are either blanks, or when there are only two values in a row.

    Furthermore if I want to cut and paste rows into sheet 3, when values from column H to CS either have values of 100 or above, 3 times in a row, or when values between 0 and 5 occurs 3 times in a row.

    Any ideas on how to go about this?

    BR

  2. #2
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Macro that can cut paste rows, when values are blanks or only show two values in a row

    For first problem try (UNTESTED)-
    Please Login or Register  to view this content.
    Check attached. Press blue button for results.
    Happy to Help

    How to upload excel workbooks at this forum - http://www.excelforum.com/the-water-...his-forum.html

    "I don't get things easily, so please be precise and elaborate"

    If someone's post has helped you, thank by clicking on "Add Reputation" below the post.
    If your query is resolved please mark the thread as "Solved" from the "Thread Tools" above.

    Sourabh

  3. #3
    Registered User
    Join Date
    01-12-2016
    Location
    Europe
    MS-Off Ver
    MS 2013
    Posts
    48

    Re: Macro that can cut paste rows, when values are blanks or only show two values in a row

    Can you run me through your code? That would be a big help. Also, how do I delete the rows in sheet1, that have blanks, or less than 2 values?

  4. #4
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Macro that can cut paste rows, when values are blanks or only show two values in a row

    Sorry, I committed a small error, this would delete the rows also..
    Please Login or Register  to view this content.

  5. #5
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Macro that can cut paste rows, when values are blanks or only show two values in a row

    This is how the code works-
    Please Login or Register  to view this content.
    l1 is the last row on Sheet1

    Please Login or Register  to view this content.
    The macro runs for every value from k= 1 to k= l1 i.e. the last row number

    Please Login or Register  to view this content.
    Instead of going from all rows from 1 down to 956 we decide to go from 965 up to the first row.
    This is because rows have to be deleted and this would create problems with the row numbers. Like if k=3 i.e. 3rd row is deleted then the 4th row becomes the 3rd row. But macro would go for k=4 next time i.e. row 4. This means we have missed a row in between that changed form row 4 to row 3 on deleting row 3.
    So n is the row number calculated by subtracting k from l1 and adding 1
    example-
    for k=1, n=965-1+1=956
    for k=2, n=956-2+1=955
    and so on...

    Please Login or Register  to view this content.
    Checks if there are less than 3 values in columns H to CS

    Please Login or Register  to view this content.
    l2 is the last used row on sheet2 plus one i.e. the first unused row.

    Please Login or Register  to view this content.
    Puts the value from columns A to CS in Sheet2 if there are less than 3 values.
    Please Login or Register  to view this content.
    Deletes the row from Sheet1

    Please Login or Register  to view this content.
    If there are more than 2 values then the macro does nothing.

    Please Login or Register  to view this content.
    Goes for the next k and finally ends.

  6. #6
    Registered User
    Join Date
    01-12-2016
    Location
    Europe
    MS-Off Ver
    MS 2013
    Posts
    48

    Re: Macro that can cut paste rows, when values are blanks or only show two values in a row

    Thank you very much. I will look into your explanation now.

  7. #7
    Registered User
    Join Date
    01-12-2016
    Location
    Europe
    MS-Off Ver
    MS 2013
    Posts
    48

    Re: Macro that can cut paste rows, when values are blanks or only show two values in a row

    sourabhg98, can you explain the range in
    Please Login or Register  to view this content.
    . Specifically, what is n in the range, and what does it do?

    Also what about
    Please Login or Register  to view this content.
    . Why are you putting Resize(1,97). I just put it a sample, I don't know the exact number of rows that will be cutted out and pasted into sheet2.

    Finally, if you were to put the last constraint on, that is to cut and paste rows that either have a values of 100 and above, plus values that have values between 0 and 5, how would you do that?

  8. #8
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Macro that can cut paste rows, when values are blanks or only show two values in a row

    As I told macro runs from k=1 to k=965 (last row)
    But the macro unfortunately would start from 1 then 2,3,4,5,6...... till 967

    I will explain in detail both the situations. Lets us assume we had "k" instead of "n"-
    Please Login or Register  to view this content.
    (Application.CountA is to count number of cells that are not blank in the specified range)

    So macro begins from k=1,
    for k=1 we will have -
    Please Login or Register  to view this content.
    for k=2 we will have -
    Please Login or Register  to view this content.
    and so on...
    So this is how this works.

    But as I told the problem here is that if let us suppose row 2 of your spreadsheet has less than 3 values. So now it has to be deleted.

    So for k=2 this will happen-
    If Application.CountA(Sheets("Sheet1").Range("H2:CS2") < 3
    (this would check if the number of values are less than 3 or not)
    In this case we have assumed that there are less than two values.

    So now if this is true macro does this-
    Please Login or Register  to view this content.
    assigns l2 the first unused row in Sheet2 starting from row 2.

    Please Login or Register  to view this content.
    Copies values of row 2 from sheet1 to first unused row of sheet2.

    Please Login or Register  to view this content.
    Deletes the row 2 from sheet1 after copying

    Now as we have completed k=2, now macro moves to k=3. Here the problem occurs.
    As we deleted row 2 row 3 must have become row 2 now.
    But macro starts from k=3 i.e. row 3, so macro missed row 3 which became row 2 on deletion.
    So instead of going from top to bottom we instead go from bottom to the top.
    For this we calculate "n" instead of "k" by formula n = l1 - k + 1
    example-
    for k=1, n=965-1+1=965
    for k=2, n=965-2+1=964
    for k=3, n=965-3+1=963

    So now assume that 964th row has less than 3 values and has to be deleted.
    For k=2, n would be 964 i.e. 964th row will be deleted. and 965th row becomes 964th row.
    So now we move to k=3
    for k=3, n would be 963
    So we see that we don't miss any row when we go from bottom to top as when rows are deleted all the rows are shifted up so even if we delete 964th row the macro would easily move to 963rd row.

    This was the purpose behind using "n" instead of "k"

  9. #9
    Registered User
    Join Date
    01-12-2016
    Location
    Europe
    MS-Off Ver
    MS 2013
    Posts
    48

    Re: Macro that can cut paste rows, when values are blanks or only show two values in a row

    Now I understand it. Thank you very much.

+ 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 that copies & paste-special(Values) non-blank rows
    By Elieson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2015, 12:27 PM
  2. Need macro/formula to only show rows that have values above a benchmark
    By thedutchrudder in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-17-2014, 12:37 PM
  3. [SOLVED] How to copy/paste column, only cells with values (non-blanks)
    By ChristopherH in forum Excel General
    Replies: 5
    Last Post: 02-03-2014, 03:14 PM
  4. Macro Coding to show on Rows with blanks in certain Cell
    By Cupragsw in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-31-2013, 11:44 AM
  5. Macro to Copy Paste Rows Depending on a Cell Value and show values against each row
    By prkhan56 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-29-2013, 11:41 AM
  6. Copy Paste Macro with Values & Skipping Blanks
    By Blockey in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-30-2012, 08:24 AM
  7. Macro adds rows with values, not blanks
    By leaning in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-11-2011, 03:26 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