+ Reply to Thread
Results 1 to 26 of 26

Need help with creating VB code for moving rows to different sheet based on cell valu

  1. #1
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Need help with creating VB code for moving rows to different sheet based on cell valu

    Hi everyone,

    I got a VB code from this site to move rows to different sheet based on cell value. It worked amazing for once, but if I run it twice, rows in target sheet are getting replaced with new ones or getting deleted.
    Can you help me on making the code to look for next empty row?

    Thank you so much in advance for helping me. I'm new to this forum and the code that I got was an amazing except that small issue that I couldn't fix.
    Sub CopyYes()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet
    Application.ScreenUpdating = False
    ' Change worksheet designations as needed
    Set Source = ActiveWorkbook.Worksheets("Facilities Requests")
    Set Target = ActiveWorkbook.Worksheets("Completed Projects")

    Application.ScreenUpdating = False
    Target.Select

    With Target
    .Rows("2:1000").Select
    Selection.Delete Shift:=xlUp
    End With

    Source.Select
    j = 2 ' Start copying to row 1 in target sheet

    For Each c In Source.Range("L2:L1000") ' Do 1000 rows
    If c.Value = "Completed" Then
    Source.Rows(c.Row).Copy Target.Rows(j)
    Source.Rows(c.Row).Delete
    j = j + 1
    End If
    Next c

    Target.Select
    Target.Range("A1").Select

    Worksheets(1).Calculate

    'Application.CutCopyMode = False
    Source.Select
    Source.Range("C5").Select
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    It does not look like you got the code from this site. Too many "select" statements.
    Could you just explain what you want to achieve?
    Explain it short and sweet, don't leave anything out.
    BTW, pack your code in code tags please.
    How To Add Code Tags.jpg

  3. #3
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Thank you Jolivanes for replying.

    I wanna move rows which has cell value of "completed" to different sheet.

    The trouble that I have with this code is that it's overriding the already moved row instead of moving to next cell.

    Please Login or Register  to view this content.
    P.S., Hope I followed what you said. Thank you once again.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Try this on a copy of your workbook.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    It's giving out error
    error.PNG

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Can you attach a sanitized version of your workbook. By sanitized I mean no personal data. It should represent your workbook though.

  7. #7
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    I've attached the sample file.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    In the explanation in Post #1 you have "completed" with a small c and in your example code it is with a capital C which I missed.
    In the code from Post #4, change the word completed to Completed or put "Option Compare Text" as your first line.
    It would look like this.
    Please Login or Register  to view this content.
    Put the code in a copy of your workbook, save the workbook as a macro enabled (.xlsm) workbook and try the code again.
    Let us know.

  9. #9
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Thank you Jolivanes!

    The code is running but if I run the same code for second time it's overriding the previous copied version

    -
    MJ

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    That's because Column A is blank.
    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Thank you so much!

  12. #12
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Hi Jolivanes,
    Thank you for helping me with the coding. My boss was really happy with the output.
    I wanna update this code for one more attribute.
    I wanna move rows into different sheets based on project start date.
    So should I add a whole new "with" block for it?

    -
    MJ

  13. #13
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Please explain what you want to achieve.
    Same range to a different sheet, different range to same sheet, different range to different sheet or something totally different?

  14. #14
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Same range to different sheets based on the year





    Sample.PNG
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Do you want all the rows of data from each year in column "G" into it's own sheet?
    What about the headers?
    Maybe do it manually and attach the workbook again so we can see the before and after.

  16. #16
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Is this what you have in mind? See attached
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    I've shown on how excel sheet should look after I update project start date.
    Attached Files Attached Files

  18. #18
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Is that not what the attachment in Post #16 does?

  19. #19
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    I'm sorry for replying late.

    Here is how it should look like.

    -
    Thank you!

  20. #20
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Try the attachment from Post #16 and let us know what needs to be changed.

  21. #21
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Wow! This looks really amazing! Thank you so much Jolivanes!!!
    I want the copied rows to be deleted from main page.

    Also will it work if I add this program to the first one "Completed projects"?
    Last edited by mj1003; 05-28-2018 at 01:05 PM.

  22. #22
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Check if the attached is what you have in mind.
    Remember, if you run code to see if it works, make sure to use a copy of your original. There is no "Undo" button for code. Gone is gone forever.
    Attached Files Attached Files

  23. #23
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    It works in the sample sheet perfectly.
    However I recently made changes on the top row to include all my macro buttons and total values. Because of this, the code is removing the header.

    I've attached a sample sheet
    Attached Files Attached Files

  24. #24
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    Everything related to Facilities Request Sheet Row1 needs to be changed to Row2 and Row2 to Row3.

    Change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    and this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    and this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    I have not ried the changes so after changing, try it on a copy of your original first.
    You will need to go through the code so you understand what's happening with it.
    There is no doubt that you will want to make changes in the future so it'll be so much easier and faster if you understand the code.

  25. #25
    Registered User
    Join Date
    05-14-2018
    Location
    Arizona, USA
    MS-Off Ver
    2016
    Posts
    13

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    It worked perfect!
    Yeah. I am completely new to coding. I'm learning it with your codes and googling to understand.
    This is definitely a great start point for me. I learned lot about VB and macros in last 2 weeks.
    Thank you so much for helping me with this.

  26. #26
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Need help with creating VB code for moving rows to different sheet based on cell valu

    It was my pleasure.
    Good luck and enjoy

+ 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. Moving Rows into a different sheet based on a cell value
    By ajvalles1 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-02-2015, 08:10 AM
  2. [SOLVED] Moving Rows to different sheet based on cell criteria
    By SpreadsheetNublet in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-08-2015, 03:12 PM
  3. Moving rows to another sheet in the same workbook based on a cell value.
    By OpOrange in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2014, 04:37 PM
  4. VBA CODE to move rows from one sheet(1) to another sheet(2) based on data in cell
    By fozzieulo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-08-2013, 02:43 PM
  5. Replies: 9
    Last Post: 02-26-2012, 07:20 AM
  6. Replies: 0
    Last Post: 11-11-2005, 02:35 PM
  7. I want to chage a whole rows text color based on single cell valu.
    By thediamondfam in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-14-2005, 11:51 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