+ Reply to Thread
Results 1 to 5 of 5

Find value X in columnA and paste entire row above value Y in columnA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Find value X in columnA and paste entire row above value Y in columnA

    I am attempting to write a macro that will find a value (BCode) in column A and cut and paste the entire row in the row above value Class.


    Value BCode is always below value Class.

    For each Class there is always a BCode.

    There can be from 1 to 38 pairs of Class and BCode in any given sheet.

    The number of rows between Class and BCode is NOT constant.

    I would like the macro the loop through column A and paste the row with value Bcode in the row above value Class...sometimes the destination row is empty sometimes it is not. I do not want any data overwritten so if the row is not empty I need to insert a row for the data.

    Sample workbook attached with the before and after.

    Any assistance appreciated.

    Thx
    Attached Files Attached Files
    Last edited by anrichards22; 11-25-2012 at 08:53 PM.

  2. #2
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Find value X in columnA and paste entire row above value Y in columnA

    So many gurus so few answers???

  3. #3
    Registered User
    Join Date
    12-28-2009
    Location
    São Paulo, Brazil
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Find value X in columnA and paste entire row above value Y in columnA

    Hello. Try this code and see if it could help you.

    Sub MoveCodeLine()
    Dim countClass As Long, classLine As Long, codeLine As Long
    Dim k As Long, x As Long, y As Long
    
    Application.ScreenUpdating = False
    countClass = Application.CountIf([A:A], "Class")
     Do While k < countClass
      classLine = [A:A].Find(what:="Class", after:=Cells(classLine + 1, 1)).Row
       codeLine = [A:A].Find(what:="Code", after:=Cells(classLine, 1)).Row
        Rows(codeLine).EntireRow.Copy
       Rows(classLine).EntireRow.Insert
      Rows(codeLine + 1).EntireRow.ClearContents
      k = k + 1
     Loop
      
      x = 1
      y = 1
       Do
        y = Cells(x, 1).End(xlDown).Row
        x = Cells(y, 1).End(xlDown).Row
         If Cells(x, 1).Value = "" Then Exit Sub
          If x - y = 2 Then
          Rows(x - 1).Insert
          x = y + 3
         ElseIf x - y > 3 Then
          Range(Cells(y + 3, 1), Cells(x - 1, 1)).EntireRow.Delete
          x = y + 3
         End If
       Loop
    
    Application.ScreenUpdating = True
    End Sub
    Osvaldo

  4. #4
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Find value X in columnA and paste entire row above value Y in columnA

    Thank you so much Osvaldo, I have been looking for a solution for months....your code works perfectly!!

  5. #5
    Registered User
    Join Date
    12-28-2009
    Location
    São Paulo, Brazil
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Find value X in columnA and paste entire row above value Y in columnA

    Hi, Richards.
    I´m glad it helped.
    Thanks for the feedback.

+ 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