+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Filter,copy and paste in excel

  1. #1
    Valued Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    244

    Filter,copy and paste in excel

    I 'm working on an excel that contains a macro to color the cells based on the columnS.
    This part of the code works perfectly.

    Now I need another macro, that could filter the rows based on each color in different columns,copy the filtered rows from the current workshhet and paste it in a destination worksheet.

    The destination workbook contains different worksheets for rows filtered based on the color.
    eg:
    The rows with orange color(color 44) has to be pasted in the Due.Amt worksheet of the destination workbook.
    The rows with green color(color 35) has to be pasted in the Expenses worksheet of the destination workbook.
    and so on for the different colors(the coloumn name in the source worksheet and the worksheet name in the destination workbook will be the same, so the paste shoud be done respectively).

    The destination worksheet may/may not contain datas, in that case, the paste as to be done in the next available empty row.
    The previous datas should not be deleted/overwritten in the destination worksheet.

    Please find the attached workbook.

    Any help and suggestions will be helpful.
    Thanks in advance.
    Attached Files Attached Files
    Last edited by vijay2482; 06-09-2011 at 01:24 PM.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  2. #2
    Registered User
    Join Date
    06-07-2011
    Location
    Raleigh, Nc
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Filter,copy and paste in excel

    have you tried recording a macro, that just filters the data? you can then look into it and see the line(s) that it uses to filter the data and then adjust accordingly with the colors.

    as far as pasting the data in the next available row, i have always found this range line to be extremely useful:

    range("A" &rows.count).end(xlup).offset(1).value
    it tells excel to go all the way down to the last cell in the "A" column (1048576 rows are in excel 2007), then come up to the last cell with data in it, then offset one.

    hope this helps!

  3. #3
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Filter,copy and paste in excel

    Hi,

    Give this a try,

    Post back any issues.

    .
    Attached Files Attached Files
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  4. #4
    Valued Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    244

    Re: Filter,copy and paste in excel

    Thanks a lot for the code.

    I get the following error:

    run time error 9
    script out of range

    I get this error in the below line:
    Set Thatwb = Workbooks("C:\destination.xlsx")
    I have added the "C:\" to set the path of the excel location.

    Thanks in advance.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  5. #5
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Filter,copy and paste in excel

    Quote Originally Posted by vijay2482 View Post
    Thanks a lot for the code.

    I get the following error:

    run time error 9
    script out of range

    I get this error in the below line:
    Set Thatwb = Workbooks("C:\destination.xlsx")
    I have added the "C:\" to set the path of the excel location.

    Thanks in advance.
    This pcs of code set the workbook "to"... this was the workbook name in your post example.You need to change the name of the destination.xlsx to whatever the real name is.

    However for example if the workbook with the 4 sheets you want to copy the info to is located on your C:\ Drive and Not open when you want to run the code, then use this line in place of the line above

    Set Thatwb = Workbooks.Open("C:\destination.xlsx")
    Post back any issues,

    .
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  6. #6
    Valued Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    244

    Re: Filter,copy and paste in excel

    Thanks for your reply.

    I modified the code as you said.
    Now I get the below error:

    Run time error 1004
    Auto filter method of range class failed.

    Line of code that produced error:
    Range("A2:P2").AutoFilter: Range("P2").AutoFilter Field:=16, Criteria1:="44"
    Last edited by vijay2482; 06-08-2011 at 07:50 AM.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  7. #7
    Valued Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    244

    Re: Filter,copy and paste in excel

    ..........
    Last edited by vijay2482; 06-10-2011 at 08:50 AM.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  8. #8
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Filter,copy and paste in excel

    Quote Originally Posted by vijay2482 View Post
    Thanks for your reply.

    I modified the code as you said.
    Now I get the below error:

    Run time error 1004
    Auto filter method of range class failed.

    Line of code that produced error:
    Range("A2:P2").AutoFilter: Range("P2").AutoFilter Field:=16, Criteria1:="44"
    in the sample workbook (The workbook you placed the code in) you only have one sheet and that sheet is the first sheet in the workbook correct?

    If so the first row is blank and the second row has your headers and your data starts on row 3 correct?

    if so when you open this sample workbook to run the code is there already a filter in place?

    When you step through the code on the sample workbook does the Destination workbook open now?

    Is there any data in column (P) ? Is the sheet protected?


    Just trying to see what you are seeing.

    .
    Last edited by realniceguy5000; 06-08-2011 at 11:12 AM.
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  9. #9
    Valued Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    244

    Re: Filter,copy and paste in excel

    answers to your questions:

    in the sample workbook (The workbook you placed the code in) you only have one sheet and that sheet is the first sheet in the workbook correct? YES

    If so the first row is blank and the second row has your headers and your data starts on row 3 correct? YES

    if so when you open this sample workbook to run the code is there already a filter in place? NO. THE destination.xlsx OPENS AND i CAN FIND THE FILTERS ON THE FIELDS IN THE Due.Amt SPREADSHEET.

    When you step through the code on the sample workbook does the Destination workbook open now? YES

    Is there any data in column (P) ? YES
    Is the sheet protected? DO YOU MEAN,WHETHER THE SHEET IS PASSWORD PROTECTED? IF SO, THE ANSWER IS "NO"

    Thaks in advance for any help.
    Last edited by vijay2482; 06-08-2011 at 11:23 AM.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  10. #10
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Filter,copy and paste in excel

    I'm not sure what is going on, it must be something with your original workbooks.

    Did you happen to try opening up the destination workbook from your 1st post and open my copy of Sample from post 3 and try to run the code with just those 2 workbooks?

    Was the result what you expected? If so then we need to figure out what the difference is between the posted workbooks and the originals.

    .
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  11. #11
    Valued Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    244

    Re: Filter,copy and paste in excel

    I used the copy of sample workbook and the destination workbook from my PC.
    I will try to use the copy of sample and destination my the post and compile and get back to you with the results tomorrow.

    Should the name of the source file be sample.xls or copy of sample.xls?

    Thanks a lot for spending your time to help.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  12. #12
    Valued Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    244

    Re: Filter,copy and paste in excel

    I tested with the copy of sample and destination.xlsx files and I get the below error at:
    "select method of range class failed"

    at the following line:
    .Range("A2").Select
    Dont know where I'm going wrong.
    I have attached the files.
    The P column in destination containes the color index value during exceution and later produced the error and then when I saved the file I get this as the value in the cells of column P.
    "NAME ?"
    Please help to identify and solve this issue.
    Thaks in advance.
    Attached Files Attached Files
    Last edited by vijay2482; 06-09-2011 at 03:20 AM.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  13. #13
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Filter,copy and paste in excel

    Hi,

    Add this line

    Thiswb.Activate
    After :

    Sub Filter1()
    Dim Thiswb As Workbook
    Dim Thatwb As Workbook
    Dim ThisLrow As Long
    Dim ThatLrow As Long
    Dim R1 As Range, R2 As Range, R3 As Range, R4 As Range
    Dim WKS As Worksheet
    Application.ScreenUpdating = False
     
    Set Thiswb = ThisWorkbook
    Set Thatwb = Workbooks.Open("C:\destination.xlsx")
     
    'PLACE NEW CODE HERE
    .
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  14. #14
    Valued Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    244

    Re: Filter,copy and paste in excel

    Thanks a lot Mike.

    It works perfect..

    I have a question:
    Should I use the below lines in all the sub's(Filter2,Filter3,Filter4) or only in Filter1 sub?
    Set Thatwb = Workbooks.Open("C:\destination.xlsx")
    Thiswb.Activate
    One more question:
    Where there are n number of sheets in the excel wotkbook and I I want to run the same Filter1 sub once for each sheet, do I need to make any changes in the existing code?
    Please find the attached sample file with 3 sheets.

    Thanks for assisting so far...
    Attached Files Attached Files
    Last edited by vijay2482; 06-09-2011 at 08:12 AM.
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  15. #15
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: Filter,copy and paste in excel

    Should I use the below lines in all the sub's(Filter2,Filter3,Filter4)?

    No, You only need to use Activate the one time, Because we changed the code to open the destination workbook when that takes place that makes the destination workbook the activeworkbook and thats why the code wasn't working. Now that we activated the sample workbook the code should loop through with out an issue...


    Where there are n number of sheets in the excel wotkbook and I I want to run the same Filter1 sub once for each sheet, do I need to make any changes in the existing code?
    I will need to make some additional changes for this.

    I'll post back Shortly.

    .
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

+ 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.2.0