• Search in macro (Excel 2003)

    Author
    Topic
    #456437

    How do i write a macro to search through an Excel file for “xxx” in COL A, move over to COL D and move the cell in D and the cell underneath it down 2 rows, go back up 2 rows and delete 2 rows and then continue? Thanks for the help.

    Viewing 0 reply threads
    Author
    Replies
    • #1140556

      Try

      Sub TestCode()
      Dim oCell As Range
      Set oCell = Range("A:A").Find(What:="xxx", _
      LookIn:=xlValues, LookAt:=xlWhole)
      Do While Not oCell Is Nothing
      oCell.Offset(0, 3).Resize(2, 1).Copy _
      Destination:=oCell.Offset(2, 3)
      oCell.Resize(2, 1).EntireRow.Delete
      Set oCell = Range("A:A").FindNext
      Loop
      End Sub

      • #1140773

        Wrong answer Hans!

        jha’s question was [indent]


        How do i write a macro …


        [/indent]Your reply showed how Hans would write such a macro.

        Cheers,
        Paul Edstein
        [Fmr MS MVP - Word]

        • #1140889

          >Wrong answer Hans!
          Wrong answer Macropod! hello
          jha’s question was
          How do i write a macro …
          Obviously, he doesn’t, otherwise he wouldn’t be asking.

          JHA: I suspect a better answer might be

          • Tools, Macro, Record
          • Turn on relative addressing
          • Perform the steps for a found “xxx” value (copy, move across/up/down/paste
          • Tools, Macro, stop Recording[/list]Then edit the macro to insert the test for “xxx”
      • #1140783

        Slight improvement to avoid error:

        Sub TestCode()
            Dim oCell As Range
            On Error Resume Next
            Set oCell = Range("A:A").Find(What:="xxx", _
                LookIn:=xlValues, LookAt:=xlWhole)
            Do While Not oCell Is Nothing
                oCell.Offset(0, 3).Resize(2, 1).Copy _
                    Destination:=oCell.Offset(2, 3)
                oCell.Resize(2, 1).EntireRow.Delete
                Set oCell = Range("A:A").FindNext
            Loop
        End Sub
        
    Viewing 0 reply threads
    Reply To: Search in macro (Excel 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: