+ Reply to Thread
Results 1 to 11 of 11

Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

  1. #1
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    I have attached a sample spreadsheet (2013-Aug Recon Sample.xlsx) that contains various dates in a month (the spreadsheet will vary as to the actual number of worksheets). Assuming all worksheets could contain a varying number of rows, I need to look at Column G on each worksheet, and if the row has data and Column G is blank, then that row needs to be copied to a clean worksheet starting at A2 (using the same column names as the current worksheets) and then rename the new worksheet as "NoRecons". It should loop through each worksheet copying those rows where Column G is blank to the new worksheet "NoRecons".

    I have attached a sample of what rows I manually copied from the 2013-Aug Recon Sample.xlsx into a separate spreadsheet so you can see the results (2013-Aug NoRecon Sample.xlsx). I am not an expert in VBA so I am having difficulty automating this task. Can someone help? Thanks in advance for your expertise.
    Attached Files Attached Files

  2. #2
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    Try the below code.....
    Please Login or Register  to view this content.
    I hope this helps.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    Another one.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    The code runs, but breaks (Subscript out of Range) at: lrnc = Sheets("NoRecon").Cells(Rows.Count, 1).End(xlUp).Row + 1
    Any ideas?

  5. #5
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    Quote Originally Posted by Ellen 2Excel View Post
    The code runs, but breaks (Subscript out of Range) at: lrnc = Sheets("NoRecon").Cells(Rows.Count, 1).End(xlUp).Row + 1
    Any ideas?
    Make sure that you have named one of your sheets as NoRecon. Otherwise I see no reason for error in that line as it was successfully executed when I ran this code.

  6. #6
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    My last sheet is named NoRecon, but I still receive the subscript out of range. See screenshot attached -
    11-6-2013 9-18-00 AM.gif

  7. #7
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    Have you tried AB33's code? Does that run well?
    Actually I use Excel 2007 and you use 2010. When I ran the code very first time, I also got strange values for lr in the code. So I deleted all your empty rows from the sheets and then the code ran successfully. I think it has got something to do with the formatting you have applied to the sheets. Do one thing, you can try what I did, i.e. deleting all the empty rows in all the sheets and then try to run the code. See if it runs smoothly and if so, there is something strange about the formatting of the sheets which I am not sure about.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    sktneer,
    The way I see, there are 2 minor issues with your code.
    If the first active sheet happens to be sheet "NoRecon", the code will terminate, so no execution.
    The second issue is
    Please Login or Register  to view this content.
    will not work with table (Lists).
    This will find the next empty row in a range, not a list (Table). sheet "NoRecon" is a table.

  9. #9
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    @AB33

    Your both points are correct. I assumed that the NoRecon would be the last sheet on the workbook. And not its clear that why lrnc is giving an error because the sheet is actually a table. Thanks for making it clear.
    Last edited by sktneer; 11-06-2013 at 02:33 PM.

  10. #10
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    This one works correctly. Thank you for taking the time to help me. I really appreciate it.

  11. #11
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Need help writing Excel VBA code to copy and paste specific rows into a new worksheet

    Yes, I just tried AB33's code and it seems to work. Thank you for trying to help me out. I appreciate your time.

+ 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. Use VBA code to copy and paste a specific selection
    By Joe Walsh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-08-2013, 05:11 PM
  2. [SOLVED] Code attaches copy of Excel worksheet to e-mail but needs to have specific Name!
    By Michelle Deigh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-05-2013, 06:44 PM
  3. [SOLVED] Macro code to Copy specific rows into specific columns
    By macrofan2012 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-11-2012, 11:24 AM
  4. Need VBA Code to copy paste rows from worksheet to another
    By amasulli in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2012, 02:16 PM
  5. [SOLVED] COPY AND PASTE SPECIFIC ROWS
    By HERNAN in forum Excel General
    Replies: 2
    Last Post: 08-17-2006, 02:35 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