+ Reply to Thread
Results 1 to 11 of 11

Macro to copy row from one sheet to another based on criteria

  1. #1
    Registered User
    Join Date
    08-15-2014
    Location
    Philippines
    MS-Off Ver
    2007
    Posts
    10

    Macro to copy row from one sheet to another based on criteria

    Hi,

    I know this has been covered a lot but I just can't seem to get my VBA Code working.

    I have a workbook that contains 5 sheets.

    The sheet with data is called, "HOSPITAL SIT REGISTER" and the data is entered between B7:U61. The 'U' column is a formula that returns yes/no dependant on the entry of other values.

    Where the answer is yes to 'U' I want it to copy the whole row into a second sheet, 'Datasets' from A18.

    Currently I have this code set up;

    Option Explicit

    Sub copy_dbo()
    Dim i As Long, lrow As Long

    Application.ScreenUpdating = False

    With Worksheets("HOSPITAL SIT REGISTER")
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    For i = 2 To lrow
    If .Range("U7:U61" & i).Value = "Yes" Then .Rows(i).Copy Worksheets("Datasets").Range("A18" & Rows.Count).End(xlUp).Offset(18, 0)
    Next i
    End With

    MsgBox "Done"

    Application.ScreenUpdating = True

    End Sub
    It completes and comes up with "Done" but it is not giving the results and there is nothing on the Datasets page.

    Also I believe I may have a problem once it works as I need it to clear any previous values each time before running this code.

    Thanks

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Macro to copy row from one sheet to another based on criteria

    are you able to mock up some data to test with
    save me data entering something up which may or may not be correct...
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    08-15-2014
    Location
    Philippines
    MS-Off Ver
    2007
    Posts
    10

    Re: Macro to copy row from one sheet to another based on criteria

    Hi,

    Find attached. As you can see I have had to remove most of the data for confidentiality reasons however I have left the dates and times in place.

    As you can see there is the column 'U' at the end which will eventually be hidden as a helper column. There is also two sections, one for metro and one for regional. I will need these two sections with 'Yes' in the U column in separate places. Metro will never have more than 7 'Yes' at any time and Region will never have more than 2 'Yes' at any time.

    Hope this makes sense.

    Thanks,
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Macro to copy row from one sheet to another based on criteria

    first thing...your lrow is looking at column A....which is blank

    is that by design?

  5. #5
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Macro to copy row from one sheet to another based on criteria

    secondly
    it seems your code calls for the whole row to be copied across...are you sure you want to do that?
    it looks like you have a heap of formulas in place for on HOSPITAL SIT DATA SHEET from Column N on wards
    are you sure you want to be over writing these?

  6. #6
    Registered User
    Join Date
    08-15-2014
    Location
    Philippines
    MS-Off Ver
    2007
    Posts
    10

    Re: Macro to copy row from one sheet to another based on criteria

    This is not by design. I must apoligise I have just been trying to copy codes as I am new to using VBA.

    The HOSPITAL SIT DATA SHEET won't be touched by this and only updates from the HOSPITAL SIT REGISTER sheet. But yes I need from the row from column B to U copied into the datasets.

    Thanks,

  7. #7
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Macro to copy row from one sheet to another based on criteria

    Please Login or Register  to view this content.
    not sure what you were trying to achieve with
    Worksheets("Datasets").Range("A18" & Rows.Count).End(xlUp).Offset(18, 0)

    also

    if you wanting to append onto datasets then because column A is empty
    when you copy data across ...end(xlup) goes up to row 15


    i changed the code to work with this attached example
    if in real data something is different let me know

  8. #8
    Registered User
    Join Date
    08-15-2014
    Location
    Philippines
    MS-Off Ver
    2007
    Posts
    10

    Re: Macro to copy row from one sheet to another based on criteria

    Thanks, that worked as required. What would I need to enter to clear the area before running to copy and paste. As each day the data will change.

    Thanks,

  9. #9
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Macro to copy row from one sheet to another based on criteria

    Please Login or Register  to view this content.
    Code if you want to append...however you need something in cell (anything) B17 to start otherwise it will start in row 3

  10. #10
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Macro to copy row from one sheet to another based on criteria

    Quote Originally Posted by jack.stapleton View Post
    Thanks, that worked as required. What would I need to enter to clear the area before running to copy and paste. As each day the data will change.

    Thanks,
    the 1st code writes over
    the 2nd code appends below

    if you want to hard "wipe" it clean
    just add some code in to do so

    maybe something like
    Worksheets("datasets").Rows("17:30").Clear

    at the beginning

  11. #11
    Registered User
    Join Date
    08-15-2014
    Location
    Philippines
    MS-Off Ver
    2007
    Posts
    10

    Re: Macro to copy row from one sheet to another based on criteria

    Brilliant Thanks

+ 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] Macro to Copy Rows to new sheet based on Multiple Criteria
    By ecubersax in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-07-2015, 01:07 AM
  2. Macro to copy row title to a different sheet based on multiple column criteria
    By Steveb03 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-05-2012, 04:24 PM
  3. Copy Paste cells into a new sheet based on criteria macro
    By Clessie1919 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-19-2012, 02:43 AM
  4. Need Macro to search from mater sheet and copy in another sheet based on Criteria
    By humacdeep in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 02-29-2012, 01:03 PM
  5. [SOLVED] Macro to copy and paste rows to a new sheet based on criteria
    By Belo in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-31-2010, 02:25 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