+ Reply to Thread
Results 1 to 10 of 10

Macro to search column and paste row

  1. #1
    Registered User
    Join Date
    06-08-2010
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Macro to search column and paste row

    So I've been trying to put together a code in VBA that will search column A from A1 to A500 for different words (i.e. Mason, Electric, HVAC) and then copy and paste that row in a desired location on a different sheet. The way I've been doing it is much to complicated and I KNOW there is an easier way to do this.

    Figuratively, I would like it to run like this

    Range A1:A500

    If A1 is Mason then copy & paste row into row 2 of sheet 2.
    Else If A1 is Electric then copy & paste row into row 5 of sheet 2
    Else if A1 is HVAC then copy & paste row into row 10 of sheet 2
    ,,,, and so on for the other words I need filtered.

    I also need the program to know to move down a space on the second sheet so that there it wont overlap with the other pasted rows.

    Thanks for the help
    Last edited by sieira67; 06-28-2010 at 03:51 PM.

  2. #2
    Registered User
    Join Date
    04-14-2010
    Location
    NZ
    MS-Off Ver
    2007,2010
    Posts
    86

    Re: Cant get macro to search a column and paste the row

    could something like this work:

    Please Login or Register  to view this content.
    Please forgive any errors.. very quick and dirty attempt with zero testing.

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Cant get macro to search a column and paste the row

    This example will loop through the items and paste the found rows into sheet 2.
    The list of items to find is in sheet3 starting at A10, it will copy and paste the rows from sheet 1 to sheet 2 in the order of your listed items
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    06-08-2010
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Cant get macro to search a column and paste the row

    First, thanks deamo and dave for taking the time to help out.

    Dave, your code works nearly perfectly. The only thing I need is some sort of gap between each filter. I tried some things myself to make this happen but I was unsuccessful.

    How about if I put the paste row next to the search word in sheet3?
    Last edited by sieira67; 06-11-2010 at 09:50 AM.

  5. #5
    Registered User
    Join Date
    06-08-2010
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Cant get macro to search a column and paste the row

    I tried inserting an offset before the each of the next commands at the end of daves code, but it hasn't worked. Any other suggestions?

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Cant get macro to search a column and paste the row

    This may be what you are looking for

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-08-2010
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Macro to search column and paste row

    That is perfect. Your the man Dave! The program does exactly what I needed.

    I realize there is something else I would like to add to the code to make it a little more convenient for me. I was hoping that under each of the separated sections in the second sheet, I could sum the values of column B. Im going to give the code a shot myself, but I would appreciate any help.

    Thanks

  8. #8
    Registered User
    Join Date
    06-08-2010
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Macro to search column and paste row

    I have been searching the web and trying to figure out a solution to this. I wrote a code that inserts sumif formulas into desired cells next to the copied rows on the second sheet, but I cant figure out whats wrong with my syntax. I tried the following line but the quotations in the criteria part of my formula keep on making an error message appear. Im not sure how to handle this or if there is a better way. I would really appreciate any help.

    Please Login or Register  to view this content.

  9. #9
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Macro to search column and paste row

    If "Site Work" is what you are looking for, Double Quotes may be required ""Site Work""

    If you start a line with an open Quote the next quote will be considered a closing quote in VBA. That's why you would need a double quote.
    Last edited by davesexcel; 06-21-2010 at 07:24 PM.

  10. #10
    Registered User
    Join Date
    06-08-2010
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Macro to search column and paste row

    Your the man dave

+ 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