+ Reply to Thread
Results 1 to 7 of 7

Moving rows to a different worksheet based on multiple criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey
    MS-Off Ver
    2013
    Posts
    47

    Moving rows to a different worksheet based on multiple criteria

    Hi all,

    I am using the following code to move rows from my worksheet if the cell in column K = Complete.

    I need to also move rows if the cell in column K = Cancelled. The following is the code I am using. I'm not great with VBA yet so i am having a problem adding the 2nd criteria of cancelled. Can anyone tell me how to amend my code to include Cancelled as a critreria?

    Sub CopyRequests()
    
    Worksheets("SOR Requests").Unprotect Password:="DC2"
    Worksheets("Completed SOR's").Unprotect Password:="DC2"
    
        Dim xRg As Range
        Dim xCell As Range
        Dim I As Long
        Dim J As Long
        Dim lngR As Long
        
        I = Worksheets("SOR Requests").UsedRange.Rows.Count
        J = Worksheets("Completed SOR's").UsedRange.Rows.Count
        
        If J = 1 Then
            If Application.WorksheetFunction.CountA(Worksheets("Completed SOR's").UsedRange) = 0 Then J = 0
        End If
        
        Application.ScreenUpdating = False
        
        For lngR = I To 10 Step -1
            If Application.CountIf(Worksheets("SOR Requests").Rows(lngR), "Complete") > 0 Then
                    With Worksheets("SOR Requests").Rows(lngR)
                    Worksheets("Completed SOR's").Rows(J + 1).Value = .Value
                    .Delete
                End With
                J = J + 1
            End If
        Next lngR
        
         
        Application.ScreenUpdating = True
        
        Range("B10:B200").Formula = "=IF(A10="""","""",WORKDAY(A10,9,Holidays!$A$1:$A$8))"
        Range("C10:C200").Formula = "=IF(A10="""","""",NETWORKDAYS($A$1,B10,Holidays!$A$1:$A$8))"
        
    Worksheets("SOR Requests").Protect Password:="DC2"
    Worksheets("Completed SOR's").Protect Password:="DC2"
    
    End Sub
    Attached Files Attached Files
    Last edited by tfagan1978; 03-23-2017 at 11:35 AM. Reason: adding a sample workbook

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Moving rows to a different worksheet based on multiple criteria

    Hi Tfagan1978

    Are you sure this is the correct code posted. Why not upload a sample workbook.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey
    MS-Off Ver
    2013
    Posts
    47

    Re: Moving rows to a different worksheet based on multiple criteria

    Hi Sintek,

    I have posted a sample workbook. The code is definitely the one I am using. I hope this helps.

    Thanks,

  4. #4
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Moving rows to a different worksheet based on multiple criteria

    The easiest fix I can think of is to replace this line:

    If Application.CountIf(Worksheets("SOR Requests").Rows(lngR), "Complete") > 0 Then
    With this line:

    If Application.CountIf(Worksheets("SOR Requests").Rows(lngR), "Complete") + Application.CountIf(Worksheets("SOR Requests").Rows(lngR), "Cancelled") > 0 Then

  5. #5
    Registered User
    Join Date
    03-01-2016
    Location
    New Jersey
    MS-Off Ver
    2013
    Posts
    47

    Re: Moving rows to a different worksheet based on multiple criteria

    This worked perfectly. Thank you

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,245

    Re: Moving rows to a different worksheet based on multiple criteria

    Hi tfagan,

    This looks like an Advanced Filter problem to me, more than a VBA problem. Learn a little about Advanced Filters using a filter of Complete OR Canceled.

    http://www.excel-easy.com/examples/advanced-filter.html
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,277

    Re: Moving rows to a different worksheet based on multiple criteria

    Try this for starters.

    Set ws1 = Sheets("SOR requests")
    Set ws2 = Sheets("Completed SOR's")
    I = Application.CountA(ws1.Columns(1)) + 1
    J = Application.CountA(ws2.Columns(1)) + 5
    For lngR = I To 10 Step -1
        If ws1.Cells(lngR, 11) = "Completed" Or ws1.Cells(lngR, 11) = "Cancelled" Then
            ws2.Cells(J + 1, 1).Resize(, 16).Value = ws1.Cells(lngR, 1).Resize(, 16).Value
            ws1.Rows(lngR).Delete
        End If
    Next
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

+ 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. How to copy rows based on multiple criteria from one worksheet to another ?
    By Caster in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2015, 06:27 AM
  2. Moving data from one worksheet to another based on criteria
    By dwhite30518 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2014, 07:53 AM
  3. [SOLVED] Mark or Delete Rows based on Matching Multiple Criteria in another worksheet
    By clearbluez in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-11-2012, 02:38 PM
  4. [SOLVED] Moving rows to a different sheet based on yes/no criteria
    By mpc_2012 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-25-2012, 06:01 AM
  5. Replies: 6
    Last Post: 11-29-2010, 04:04 AM
  6. Moving rows to separate worksheet if criteria is met
    By lee4clp in forum Excel General
    Replies: 16
    Last Post: 08-11-2010, 12:05 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