+ Reply to Thread
Results 1 to 10 of 10

Copy function before inserting row corrupting macro

  1. #1
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Copy function before inserting row corrupting macro

    Hi there,

    I want to make a code that copies a range when conditions are met and then iserts the row below these conditions, selects cell in new row and paste copied selection.

    When I am doing insert row when conditions are met then it seems to work i.e



    Please Login or Register  to view this content.


    But the problem starts when I add the other conditions so I have :


    Please Login or Register  to view this content.
    Seems like it is adding a row but pasting automatically copied cells from first condition into cell ("A",i).






    Next
    Last edited by nosense; 08-19-2015 at 07:35 AM.

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Copy function before inserting row corrupting macro

    I may be wrong but you appear to be doing this in two seperate loops which will cause you problems.

    Imagine the 1st loop finds 5 matching items, the 1st at row 10 so it adds in a new row at 11 (so the old row 11 becomes 12), it then does the same at rows 20,30,40 and 50

    Your 2nd loop should really now loop from 505 to 9 as there are now an additional 5 rows in there that werent before the 1st loop ran.

    Im not sure if this is the reason for your problem but its a problem in itself.

    You should be carrying out both actions in the same IF statement so theyre done as one. You probably also dont need to insert a row as a seperate action.

    Your best bet is to do the copy and paste actions on a test row while recording a Macro then amend the code in there to fit into your IF statement.
    If someone has helped you then please add to their Reputation

  3. #3
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: Copy function before inserting row corrupting macro

    Quote Originally Posted by pjwhitfield View Post
    I may be wrong but you appear to be doing this in two seperate loops which will cause you problems.

    Imagine the 1st loop finds 5 matching items, the 1st at row 10 so it adds in a new row at 11 (so the old row 11 becomes 12), it then does the same at rows 20,30,40 and 50

    Your 2nd loop should really now loop from 505 to 9 as there are now an additional 5 rows in there that werent before the 1st loop ran.



    Im not sure if this is the reason for your problem but its a problem in itself.

    You should be carrying out both actions in the same IF statement so theyre done as one. You probably also dont need to insert a row as a seperate action.

    Your best bet is to do the copy and paste actions on a test row while recording a Macro then amend the code in there to fit into your IF statement.
    Thanks for the reply. Your sugesstion is invalid as nature is that there is always only one entry that meets conditions.

    If you look at the code, it is first finding the row that meets conditions and copies a range from this row.

    Please Login or Register  to view this content.
    Second "IF" is inserting entire row below the row that meets same conditions

    Please Login or Register  to view this content.
    Problem is that "insert row" macro is working fine if the first statement is not there which does not make sense because this firsts statement only copies the range
    Last edited by nosense; 08-18-2015 at 06:49 AM.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy function before inserting row corrupting macro

    Ditto pjwhitfield try:

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: Copy function before inserting row corrupting macro

    Quote Originally Posted by JOHN H. DAVIS View Post
    Ditto pjwhitfield try:

    Please Login or Register  to view this content.
    This did help but is applying it to condition "As wound" instead of both (AND).

    Another big problem is that it does not keep the formulas equivalently for this row....
    any way of doing this?
    Cheers

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy function before inserting row corrupting macro

    Can you attach a sample workbook?

  7. #7
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Copy function before inserting row corrupting macro

    Quote Originally Posted by nosense View Post
    Thanks for the reply. Your sugesstion is invalid as nature is that there is always only one entry that meets conditions.

    If you look at the code, it is first finding the row that meets conditions and copies a range from this row.

    Please Login or Register  to view this content.
    Second "IF" is inserting entire row below the row that meets same conditions

    Please Login or Register  to view this content.
    Problem is that "insert row" macro is working fine if the first statement is not there which does not make sense because this firsts statement only copies the range
    Its highly valid.

    Your code currently runs through 492 rows, from row 9 to row 500 and everytime it finds a matching entry it copies some data...however it can only hold one set of copied data so we'll say it finds a match at row 50, it copies the range "C50: K50" and then moves to the next row and so on....it then finds another match at row 120 so it copies "C120:K120" data but to do this it loses what it had copied from the earlier row. If they are the only two rows then its holding the data from row 120 in memory.

    You then run a backwards loop from 500 to 9 and whenever that criteria is matched you're inserting what you copied however you're
    a) not telling it where to paste it so its always going to go into the start of the row, if you want it somewhere specific then you'll need to tell it that. eg
    Please Login or Register  to view this content.
    to paste it into column C onwards.

    b) though you're always going to paste in the values from the last row that was copied so if you've got 10 matching rows then all of them are going to get the details from the 10th one pasted in.

    As John posted the code for, you should do both actions at the same time ie

    IF ((x = y) AND (white = black)) THEN
    COPY THE DATA
    INSERT THE DATA
    END IF


    You need to post your workbook with a sample sheet showing what is there before and some data showing what you expect.

  8. #8
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: Copy function before inserting row corrupting macro

    1.xlsm

    Quote Originally Posted by JOHN H. DAVIS View Post
    Can you attach a sample workbook?
    Here is a sample file.

    Please Login or Register  to view this content.
    Seems like only only second "IF" conditions is being accounted. When i add the line

    Please Login or Register  to view this content.
    .
    It inserts "text" to all rows instead of the one for which condition is met.

    I need the new inserted row to keep all formulas too

    Cheers,

    Stan
    Last edited by nosense; 08-19-2015 at 08:12 AM.

  9. #9
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: Copy function before inserting row corrupting macro

    Quote Originally Posted by pjwhitfield View Post
    Its highly valid.

    Your code currently runs through 492 rows, from row 9 to row 500 and everytime it finds a matching entry it copies some data...however it can only hold one set of copied data so we'll say it finds a match at row 50, it copies the range "C50: K50" and then moves to the next row and so on....it then finds another match at row 120 so it copies "C120:K120" data but to do this it loses what it had copied from the earlier row. If they are the only two rows then its holding the data from row 120 in memory.

    You then run a backwards loop from 500 to 9 and whenever that criteria is matched you're inserting what you copied however you're
    a) not telling it where to paste it so its always going to go into the start of the row, if you want it somewhere specific then you'll need to tell it that. eg
    Please Login or Register  to view this content.
    to paste it into column C onwards.

    b) though you're always going to paste in the values from the last row that was copied so if you've got 10 matching rows then all of them are going to get the details from the 10th one pasted in.

    As John posted the code for, you should do both actions at the same time ie

    IF ((x = y) AND (white = black)) THEN
    COPY THE DATA
    INSERT THE DATA
    END IF


    You need to post your workbook with a sample sheet showing what is there before and some data showing what you expect.
    Thanks, I have posted the workbook, Can you please have a look ?

  10. #10
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Copy function before inserting row corrupting macro

    Sorry, Im not seeing the workbook anywhere?

+ 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. Excel is corrupting my file after saving a worksheet I ran my macro on??
    By radius1080 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-24-2015, 10:13 AM
  2. Replies: 0
    Last Post: 05-20-2014, 11:58 AM
  3. Worksheet_Change corrupting formula
    By blastronaut in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-13-2011, 04:27 PM
  4. Inserting rows function/macro?
    By farzyness in forum Excel General
    Replies: 0
    Last Post: 08-24-2010, 02:17 PM
  5. inserting cells into a hyperlink, inserting link via macro
    By jackmc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2007, 12:02 AM
  6. [SOLVED] Adding a Function to a cell when inserting a row from a macro
    By Mbarnes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-31-2006, 11:45 AM
  7. [SOLVED] VB Corrupting Pivot Tables
    By RestlessAde in forum Excel General
    Replies: 3
    Last Post: 08-06-2005, 09:05 AM

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