+ Reply to Thread
Results 1 to 11 of 11

Macro to duplicate line

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Macro to duplicate line

    Hi all,

    Im hoping you can help me out......

    Anything more than recording a macro and Im way out of my depth!

    I have a spreadsheet (when I try to upload it says its corrupt)

    A - B - C
    1 Loosen wheel nuts - Operator -
    2 Jack up car - Operator -
    3 Inspect jack up - Inspector - Random
    4 Remove nuts - Operator -
    5 Remove wheel - Operator -
    6 Inspect removal of wheel - Inspector - Random
    7 Replace wheel - Operator -
    8 Replace nuts - Operator -
    9 Lower car - Operator -
    10 Tighten nuts - Operator -
    11 Inspect tighten of nuts - Inspector - Mandatory



    Where a random inspection occurs (ie row 3) I want to insert a line and copy the row row above, so that it duplicates the inspection. Where a mandotory inspection occurs I dont want anything to happen.

    Is this even possible as when I record a macro it remembers the row number each time so when I have a different sequence, it inserts the row at the wrong point.

    Thank you for any help
    Alex

  2. #2
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Macro to duplicate line

    Hi

    could you put in the output that you want with this explanation?
    Click *, if my suggestion helps you. Have a good day!!

  3. #3
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Macro to duplicate line

    Hi,

    Sure.....

    At row 3 for example, I would hope to see a new row being inserted above or below the "random" and then the "random" rows copied into the new row as per below. Hope that makes sense! I need to copy the entire row though

    A - B - C
    1 Loosen wheel nuts - Operator -
    2 Jack up car - Operator -
    3 Inspect jack up - Inspector - Random
    3 Inspect jack up - Inspector - Random
    4 Remove nuts - Operator -
    5 Remove wheel - Operator -
    6 Inspect removal of wheel - Inspector - Random
    6 Inspect removal of wheel - Inspector - Random
    7 Replace wheel - Operator -
    8 Replace nuts - Operator -
    9 Lower car - Operator -
    10 Tighten nuts - Operator -
    11 Inspect tighten of nuts - Inspector - Mandatory

    Thanks for your help

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to duplicate line

    hi cossie2k, please check attachment, press Run button
    Attached Files Attached Files

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro to duplicate line

    Try this code
    
    Option Explicit
    
    Sub inspection()
    Dim lrow As Long, i As Long
    
    With Worksheets("Sheet1")
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            If .Range("C" & i).Value = "Random" Then
                .Rows(i + 1).Insert
                .Range("A" & i & ":C" & i).Copy .Range("A" & i + 1)
                i = i + 1
                lrow = lrow + 1
            End If
        Next i
    End With
    
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Macro to duplicate line

    Thanks Watersev / arlu1201,

    They work perfectly! Its been driving me mad for over a week!

    If its no trouble, please could you explain the component parts as some of the code makes sense, others though I dont understand how they fit in for example "& i"

    Thanks
    Alex

  7. #7
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Macro to duplicate line

    If I wanted to manually insert a column in order to add an operation number for example, what would I need to change in the macro? I assumed Id change the "C" to a "D" but this doesnt work now!

  8. #8
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Macro to duplicate line

    Can you just clarify are you talking about Arlettes code and you want it to copy extra columns but the search is still for the word random in column C or do you mean that you want it to search for the "operation number"?
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  9. #9
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Macro to duplicate line

    Sorry for the delay in getting back.

    I have now figured out how to adjust what I need to with one exception.

    I suspect Im pushing my luck here but where the duplicate row goes in, is it possible to change the inspector to operator, just for the new line.

    In effect Id like a duplicate inspection row with the first inspection against the inspector and the second row against the operator?

    Thanks for all your help
    Alex

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro to duplicate line

    Try this
    
    Option Explicit
    
    Sub inspection()
    Dim lrow As Long, i As Long
    
    With Worksheets("Sheet1")
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            If .Range("C" & i).Value = "Random" Then
                .Rows(i + 1).Insert
                .Range("A" & i & ":B" & i).Copy .Range("A" & i + 1)
                .range("C" & i+1).value = "Operator"
                i = i + 1
                lrow = lrow + 1
            End If
        Next i
    End With
    
    End Sub

  11. #11
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Macro to duplicate line

    wow!

    Thanks for the quick reply!

    Ill try it out now!

+ 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