+ Reply to Thread
Results 1 to 26 of 26

Loop through two filters and copy a cell to another sheet

  1. #1
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Question Loop through two filters and copy a cell to another sheet

    Hi,

    I'm trying to create a table that shows a certain value between two states.
    I have 50 States, so that's 2500 values I need to input and update frequently.

    How do I use the macro to loop through two filters (states 1 and 2), extract the value (cell B1), and paste that into the appropriate table cell in sheet1?

    For example, if I were to get a value for MI and OH from sheet2, I'd filter column C to 'MI' and column D to 'OH'. This returns 5 results and the resulting B1 value is 2.20. I need to copy and paste this value into sheet1's cell AI22.

    I hope this works out.
    Thank you.
    Attached Files Attached Files
    Last edited by kvnpark; 10-20-2011 at 03:24 PM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi kvnpark

    Welcome to the Forum!

    I don't particularly like this approach. You need to click on Sheet 2 Cell B1 to fire the macro.

    Place this code in Sheet 2 module
    Please Login or Register  to view this content.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Thanks for the quick reply.
    Sorry for the newbie question, but where do I exactly paste this into?
    And do I just just click on cell B1 to start?

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi kvnpark

    Right click on the Sheet 2 tab...select "View Code". This opens VBA. Paste the code in the right hand window.

    To fire the code place your cursor anywhere EXCEPT on Cell B2, do your filtering...then select Cell B2. The code will fire. You won't "see" anything but the proper Sheet 1 cell will be populated.

    There is an alternative to this...a command button could be placed on Sheet 2 and a procedure run from the button. If you're interested in this approach, let me know.

    Let me know of issues.
    Last edited by jaslake; 10-18-2011 at 03:45 PM.

  5. #5
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Thank you John!
    It works.
    Now, can we put this inside a loop so that the whole table gets generated with a single run? This is the automation that I need. A button would be nice too, but I think I can figure that out on my own.

    Thank you so much.

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi kvnpark

    I don't really know what you're asking here.
    can we put this inside a loop so that the whole table gets generated with a single run
    Please explain further.

  7. #7
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Quote Originally Posted by jaslake View Post
    Hi kvnpark

    I don't really know what you're asking here. Please explain further.
    With the code you have provided, I have to do the filtering myself for all 2500 combinations, right?
    I want to create a macro that will run through the filters on its own.
    So, something like this:

    for every State1 {
    for every State2 {
    copy sheet2's cell B1 to the corresponding cell in sheet1
    }
    }

    Hope that clarifies it

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi kvnpark

    Wow, yes that clarifies IT. The IT is a totally different animal with a totally different approach. I'd assume it can be done...need to think about how...bit more complex.

    Get back to you tomorrow I'd assume.

  9. #9
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Thanks. I'll await your response.

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi kvnpark

    Your profile indicates Excel 2010. Does this need to run on pre Excel 2007?

  11. #11
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Quote Originally Posted by jaslake View Post
    Hi kvnpark

    Your profile indicates Excel 2010. Does this need to run on pre Excel 2007?
    That would be ideal. Some computers in the office have 2003 installed. But it's not essential.

  12. #12
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Hi John,
    Any luck with the new code?

  13. #13
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi kvnpark

    Just opened the fie...get back to you.

  14. #14
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi kvnpark

    Try the code in the attached. Click the button on Sheet2. IF it does as you require, it's been tested on both Excel 2000 and Excel 2007. Let me know of issues.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    WOW! You're amazing. It works!
    I'll test it on the full set of data tonight or tomorrow and give you a feedback.

    Thanks again!
    Kevin

  16. #16
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Hi John,

    I copied and pasted more data into sheet2, and tried it. It seems to work for the most part.
    But for some reason, the row 21 of sheet1, which is MD, is blank even though there are two entries with MD as state1. Both also have corresponding state2 of 'ON'. Thus cell AL21 should have a value of 2.07.

    When I first tried with new data, the loop would stop and sheet2 would show me some filtered result. But I figured out that it was due to spaces in front of some State abbreviations (ex. " IL" instead of "IL"). I fixed all that and some other entry errors.

    So, the only major issue at the moment is the data for State1 of MD (AL21 to be specific). I can't see anything wrong with the entry. It just won't enter the value into the table.
    Attached Files Attached Files
    Last edited by kvnpark; 10-19-2011 at 09:09 PM. Reason: forgot to attach

  17. #17
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Hi John,

    I figured out when the cell AL21 is not entered. It's when I sort the data by State1 (A to Z).
    Conversely, when I sort the data by State1 "Z to A", instead of AL21, cell AL20 is blank. The cell value should be 1.24.

    When I sort the list by date (Oldest to newest), both AL21 and AL20 are filled correctly; however, now Z44 is missing.
    When I sort the list by date (Newest to oldest), both AL21 and AL20 are again filled in correctly; however, Z35 is missing.

    Basically, depending on how I sort the data in sheet2, some values don't get filled in to sheet1.

    I'm not sure how this is possible. I hope this works out.
    Thanks.
    Last edited by kvnpark; 10-20-2011 at 10:00 AM. Reason: typo

  18. #18
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi Kevin

    I'm on it. May have just discovered a clue...I'll let you know.

  19. #19
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi Kevin
    Try the code in the attached. I believe the issue is resolved. You tell me.

    PS: I meant to delete Sheet3...it's a backup copy of Sheet2.
    Attached Files Attached Files
    Last edited by jaslake; 10-20-2011 at 01:30 PM. Reason: Add PS

  20. #20
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Cool. I'll try it out and report back. Thx.

  21. #21
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    It looks good. Everything seems to be working. :D
    Could you explain a little bit about the role of the Constants tab and the two new columns?

    I'll mark the thread as solved after trying it out a little more.

    Thanks.

  22. #22
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi Kevin

    Glad it work's for you.

    If I don't get back to you on this by Monday
    Could you explain a little bit about the role of the Constants tab and the two new columns
    please remind me. I've house guests this weekend , so, will not be paying much attention to the Forum.

    PS: When you say
    two new columns
    to what are you referring?
    Last edited by jaslake; 10-20-2011 at 10:01 PM. Reason: Add PS

  23. #23
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    Quote Originally Posted by jaslake View Post
    what are you referring?
    I mean the two columns in the Constants sheet. Before, I only saw one column with header State1 generated. Now I see two more columns with State2 headers. Constants sheet in general is what I'm curious about.

    Enjoy your weekend!

  24. #24
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi Kevin

    Ah...I understand. Hopefully I can explain so you understand. There IS a reason for it. Get back to you. Again, if I don't get back to you by Monday...REMIND ME.

  25. #25
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through two filters and copy a cell to another sheet

    Hi Kevin

    The first procedure "Sub CreateUniqueStates()" creates a list of all states from Sheet2 column C. It also creates a named range (State1) for this unique list (duplicates removed). That's Column A of Sheet Constants. This named range is the VBA equivalent of what you'd see doing a manual filter on Column C. This named range is used in "Sub FilterStates()".

    The second procedure, "Sub FilterStates()", cycles each item in "State1" and filters Column C for the value of the item. That filter on Column C then creates a Column D filtered list This column D filtered list may contain 1 item or 100+ items...who knows how many. That's Column D of Sheet Constants.

    The second procedure then takes Column D of Sheet Constants and creates a UNIQUE list of Sheet Constants Column D and places it in Column G of Sheets Constants. It names the contents of Column G "State2". The second procedure cycles each item in "State2" and filters Column D for the value of the item. This is the equivalent of you manually filtering Column D.

    Hope that explains it...if not, let me know.

  26. #26
    Registered User
    Join Date
    10-18-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Loop through two filters and copy a cell to another sheet

    I think I understand the framework.
    Thank you so much once again.

    Best wishes.
    Kevin

+ 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