+ Reply to Thread
Results 1 to 6 of 6

Conditional Copy Routine

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2009
    Location
    New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    4

    Conditional Copy Routine

    I am trying to write a macro to search a column for a specific text string which when found, will copy the whole row the string is in. Once this row has been copied, I then want the macro to activate a new sheet and search for the next available empty row to paste the data. Once this has been done, go back to the original sheet and find the next cell in the original column with the specified text string and repeat until the range has been satisfied. Below is the script I have that sort of works.

    Sub MoveLostJobs()
    
    '
    ' Move Lost Jobs Macro
    ' Macro recorded 6 Oct 2009 by Phil Angove
    '
    Dim i As Integer
    Dim j As Integer
    Dim SheetName As String
    
    
    SheetName = ActiveSheet.Name
    
    For i = 71 To 86
    
        If Range("G" & i) = "Lost" Then
    
            Range("A" & i).Select
            Selection.EntireRow.Copy
            Sheets("Jobs Lost").Select
    
            j = 3
    
                If Range("A" & j) <> "" Then
    
                j = j + 1
    
                Else
    
            Range("A" & j).Select
            Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
            End If
        End If
        Next i
    
    Sheets(SheetName).Select
    End Sub
    This script copies the row information and pastes it in the correct place but does not repeat for the next cell with "Lost" in it.
    Last edited by PhilA; 10-06-2009 at 03:52 PM. Reason: Title modified by Mod

  2. #2
    Registered User
    Join Date
    10-05-2009
    Location
    New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Conditional Copy Routine

    Thank you Richard, I will try this when I get to work tomorrow and let you know how I get on.

    D.O.

    I will do my best to comply with the rules from now on.

    Phil

  3. #3
    Registered User
    Join Date
    10-05-2009
    Location
    New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Conditional Copy Routine

    Hi Richard,

    This script worked perfectly with just a slight bit of tweaking for my particular application (the from worksheet was variable depending on the user).

    Thanks again for your help.

    Phil

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: VBA Question

    Sorry to be a pain - I should have mentioned this before... could you please re-title your thread also ? The thread title should be specific to the problem at hand - ie along the lines of what you "google" (you wouldn't google "VBA Question" for ex.)

    To change a Title on your post, click EDIT on First Post then Go Advanced and change your title

    Thanks

  5. #5
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: VBA Question

    Hi Phil

    Try the following code - it uses Autofilter which removes the need to loop (and can be very fast).

    You will need to amend the wsFrom and wsTo names as indicated in the code, potentially the TO_FIND constant if you choose a different search term and also the Range (G71:G86 currently) if you wqant to extend the data rows being examined./

    Sub copy_data()
    Const TO_FIND = "Lost"  'amend as required
    Dim rng As Range
    Dim wsFrom As Worksheet, wsTo As Worksheet
    Dim lngCalc As Long
    
    With Application
        lngCalc = .Calculation
        .Calculation = xlCalculationManual
    End With
    
    Set wsFrom = Sheets("Sheet2")   'amend as appropriate (this is the name of the sheet you are copying data FROM)
    Set wsTo = Sheets("Sheet1") 'amend as appropriate (this is the name of the sheet you are copying data TO)
    
    With wsFrom
        .AutoFilterMode = False
        Set rng = .Range("G71:G86") 'this is the range  that contains the data - amend as appropriate
    End With
    With rng
        .Cells(1, 1).EntireRow.Insert
        .Offset(-1).Resize(1).Value = "TempHeader"
        .Resize(.Rows.Count + 1).Offset(-1).AutoFilter field:=1, Criteria1:=TO_FIND
        .EntireRow.Copy
        With wsTo.Cells(Rows.Count, "A").End(xlUp).Offset(1)
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
        End With
        .AutoFilter
        .Offset(-1).Resize(1).EntireRow.Delete  'delete temp hdr row
    End With
    
    Application.Calculation = lngCalc
    
    End Sub
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional Copy Routine

    Given Richard has gone to the lengths of providing a possible solution (and not wanting to waste his considerable efforts) I have on this occasion modified your thread title but I would ask that going forward you ensure your thread titles comply with the rules of the Forum.

    Thanks in advance for your co-operation
    D.O.

+ 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