+ Reply to Thread
Results 1 to 10 of 10

Cutting/Pasting Rows based on content PT 2

  1. #1
    Registered User
    Join Date
    08-24-2006
    Location
    Maryland
    Posts
    50

    Cutting/Pasting Rows based on content PT 2

    I posted a question about cutting and pasting rows based on content yesterday, and I used the code that was provided and it worked great.

    NOW I need to do it again, but this time there is a lot more criteria that I need to remove (cut and paste into another worksheet).

    I would like to focus ONLY on column B (titled PAYCL).

    Essentially, this is what I would like my macro to do:

    If the number in column B : "is between 0300 and 0399", "is between 1100 and 1199", "is between 4018 and 4028", is equal to "4011", or is equal to "4028" then cut the entire row and paste it into a new worksheet within that workbook and title it "TS"

    did that make sense?

    Attached is an example of a spreadsheet that I have to perform this task on DAILY.

    Thanks everyone, I think this will be my last post for today (you can all breathe a sigh of relief...haha)

    much respect,
    -Laura
    Attached Files Attached Files

  2. #2
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    How's this:

    Please Login or Register  to view this content.
    Col
    If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.

  3. #3
    Registered User
    Join Date
    08-24-2006
    Location
    Maryland
    Posts
    50
    Sub copy_if_matches_criteria()

    Dim wshSource As Worksheet, wshDest As Worksheet
    Dim rngCell As Range

    If Intersect([b:b], ActiveSheet.UsedRange) Is Nothing Then Exit Sub

    Set wshSource = ActiveSheet
    Set wshDest = Sheets.Add(before:=ActiveSheet)
    wshDest.Name = "TS"

    wshSource.Select
    For Each rngCell In Intersect([b:b], Range(Rows(2), Cells.SpecialCells(xlCellTypeLastCell)))
    If (rngCell.Value * 1 > 300 And rngCell.Value * 1 < 399) Or _
    (rngCell.Value * 1 > 1100 And rngCell.Value * 1 < 1199) Or _
    (rngCell.Value * 1 > 4018 And rngCell.Value * 1 <= 4028) Or _
    rngCell.Value * 1 = 4011 Then rngCell.EntireRow.Copy _
    wshDest.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    Next

    wshSource.[1:1].Copy wshDest.[a1]
    wshDest.Select

    End Sub

    OOPS. I need it to CUT the rows and then paste to a new sheet. This one was only COPYING them. Would I just change it from
    "wshSource.[1:1].Copy wshDest.[a1]
    wshDest.Select"

    to

    wshSource.[1:1].CUT wshDest.[a1]
    wshDest.Select

    ?

  4. #4
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    No, that's just copying the first row - the column titles - to the new sheet. Change "rngCell.EntireRow.Copy _" to "rngCell.EntireRow.Cut _" and put

    Please Login or Register  to view this content.
    before the line:
    wshDest.Select

    and you're laughing!

    C

  5. #5
    Registered User
    Join Date
    08-24-2006
    Location
    Maryland
    Posts
    50

    Angry

    No, that's just copying the first row - the column titles - to the new sheet. Change "rngCell.EntireRow.Copy _" to "rngCell.EntireRow.Cut _" and put


    Code:
    intersect([b:b],activesheet.usedrange).specialcells(xlcelltypeblanks).entirerow.delete
    before the line:
    wshDest.Select

    and you're laughing!

    C
    Col,

    right now, i'm ripping my hair out. It worked great, except for ONE teeny tiny row that didn't get cut and pasted over. I've tried to think of EVERY reason for that ONE stupid row to have stayed behind. I'm baffled....

    here's the code I used:

    PHP Code: 
    Sub copy_if_matches_criteria()

        
    Dim wshSource As WorksheetwshDest As Worksheet
        Dim rngCell 
    As Range
        
        
    If Intersect([b:b], ActiveSheet.UsedRangeIs Nothing Then Exit Sub
        
        Set wshSource 
    ActiveSheet
        Set wshDest 
    Sheets.Add(before:=ActiveSheet)
        
    wshDest.Name "TS"
        
        
    wshSource.Select
        
    For Each rngCell In Intersect([b:b], Range(Rows(2), Cells.SpecialCells(xlCellTypeLastCell)))
            If (
    rngCell.Value 300 And rngCell.Value 399) Or _
                
    (rngCell.Value 1100 And rngCell.Value 1199) Or _
                
    (rngCell.Value 4018 And rngCell.Value <= 4025) Or _
                
    (rngCell.Value 4028) Or _
                
    (rngCell.Value 305) Or _
                rngCell
    .Value 4011 Then rngCell.EntireRow.Cut _
                wshDest
    .Cells(Rows.Count1).End(xlUp).Offset(10)
        
    Next
        
        wshSource
    .[1:1].Copy wshDest.[a1]
        
    Intersect([b:b], ActiveSheet.UsedRange).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        wshDest
    .Select
        
    End Sub 
    and I attached the spreadsheet with that ONE stinking row that was left behind.

    any suggestions?

    you're the best.
    Attached Files Attached Files

  6. #6
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    None at all, I'm afraid... I tested it out and it worked fine, including the row that didn't move for you. If the reason comes to me I'll let you know, but hopefully one of the good people who hang around on this forum (who are, I must say, an exceptionally bright bunch) might have a clue because right now I'm baffled, sorry.


    C

  7. #7
    Registered User
    Join Date
    08-24-2006
    Location
    Maryland
    Posts
    50
    I'm getting ready to test it on another report from another day. Cross your fingers.....

    I need a good scotch after this whole mess!

    thanks buddy!

    -your grateful servant, laura

  8. #8
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301

    Bizarre behaviour

    For some reason, occasionally the For...Next loop was skipping from B2 to B4 without checking B3 - I haven't a clue why, as when I rearranged the rows in random order it didn't do it every time. But changing the Cut to Copy and then clearing the values in the row seems to solve it:

    Please Login or Register  to view this content.
    Got there in the end...


    C

  9. #9
    Registered User
    Join Date
    08-24-2006
    Location
    Maryland
    Posts
    50
    Thank you my new genius friend.....only one question.

    I'm not at work yet, so I haven't had a chance to try it out, but you said you changed the cut to copy? because i need it cut

    just curious

  10. #10
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Yeah, it copies the row then clears the cells - same effect as cutting, just an extra step


    C

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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